sinθ
sinθ

Reputation: 11493

SailsJs: How to access model from lifecycle callback?

Let's say I have a model called Twins:

var model = module.exports = {

    attributes: {
        name: {
             type: 'string',
        }
    }

    afterCreate: function(twin){
        Twins.create({
             name: twin.name + 'II'
        })
    }
}

How do I access the Twins model object or, for that matter, any Model object within a model lifecycle callback. Simply writing Twins does not work in the model file.

Upvotes: 0

Views: 301

Answers (1)

mdunisch
mdunisch

Reputation: 3697

You can access a Model like you do it. But you missed the "exec()" at your create-function so the create would never be done.

Try this:

afterCreate: function(twin){
    Twins.create({
         name: twin.name + 'II'
    }).exec(function(err,item){
       if(err) return sails.log.error(err);

       console.log(item);
    });
}

Upvotes: 2

Related Questions