antoniopol06
antoniopol06

Reputation: 81

Fields that are a relationships dont be appeared by callback create in SailsJS

I have the following Media schema

    module.exports = {
        attributes: {
            user: {
                model: 'user',
                required: true
            },
            jobs: {
                collection: 'job',
                via: 'media'
            }
        },
        afterCreate: generateJob,
    }

The method generateJob the media object is like that

{
  user: '55dcd2db428f50ea1ecf670e'
}

But job field doesnt appear and I need it because I want to associate one job to media but media doesnt get the media.jobs.add() method.

Thanks

Upvotes: 1

Views: 25

Answers (1)

user4707474
user4707474

Reputation:

In order to retrieve the data from an association, you must invoke the .populate() method; waterline does not automatically populate this data (it it is too expensive and too rarely necessary to be a core feature, in my humble opinion).

Somewhere along the line (e.g. in generateJob), you'll need to do something like this:

Media.find(...).populate('jobs'), at which point you'll be able to invoke .add() and .remove() on the associated collection.

Upvotes: 1

Related Questions