javascript - How can I call a model instance method in lifecycle callback in SailsWaterline? - Stack Overflow

I have set up a simple model with 2 instance methods. How can I call those methods in lifecycle callbac

I have set up a simple model with 2 instance methods. How can I call those methods in lifecycle callbacks?

module.exports = {

  attributes: {

    name: {
      type: 'string',
      required: true
    }

    // Instance methods
    doSomething: function(cb) {
      console.log('Lets try ' + this.doAnotherThing('this'));
      cb();
    },

    doAnotherThing: function(input) {
      console.log(input);
    }

  },

  beforeUpdate: function(values, cb) {
    // This doesn't seem to work...
    this.doSomething(function() {
      cb();
    })
  }

};

I have set up a simple model with 2 instance methods. How can I call those methods in lifecycle callbacks?

module.exports = {

  attributes: {

    name: {
      type: 'string',
      required: true
    }

    // Instance methods
    doSomething: function(cb) {
      console.log('Lets try ' + this.doAnotherThing('this'));
      cb();
    },

    doAnotherThing: function(input) {
      console.log(input);
    }

  },

  beforeUpdate: function(values, cb) {
    // This doesn't seem to work...
    this.doSomething(function() {
      cb();
    })
  }

};
Share Improve this question edited Apr 23, 2015 at 23:07 Travis Webb 15k9 gold badges58 silver badges110 bronze badges asked Oct 17, 2013 at 8:03 ragulkaragulka 4,3427 gold badges49 silver badges74 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

It looks like custom defined instance methods were not designed to be called in lifecycle but after querying a model.

SomeModel.findOne(1).done(function(err, someModel){
   someModel.doSomething('dance')
});

Link to example in documentation - https://github./balderdashy/sails-docs/blob/0.9/models.md#custom-defined-instance-methods

Try defining the functions in regular javascript, this way they can be called from the entire model file like this:

// Instance methods
function doSomething(cb) {
  console.log('Lets try ' + this.doAnotherThing('this'));
  cb();
},

function doAnotherThing(input) {
  console.log(input);
}

module.exports = {

  attributes: {

    name: {
      type: 'string',
      required: true
    }
  },

  beforeUpdate: function(values, cb) {
    // accessing the function defined above the module.exports
    doSomething(function() {
      cb();
    })
  }

};

doSomething and doAnotherThing aren't attributes, are methods and must be at Lifecycle callbacks level. Try something like this:

module.exports = {

    attributes: {

        name: {
            type: 'string',
            required: true
        }

    },

    doSomething: function(cb) {
        console.log('Lets try ' + "this.doAnotherThing('this')");
        this.doAnotherThing('this')
        cb();
    },

    doAnotherThing: function(input) {
        console.log(input);
    },

    beforeCreate: function(values, cb) {

        this.doSomething(function() {
            cb();
        })
    }

};

On Second place, you're trying send to console this.doAnotherThing('this') but it is an instance of model so you can't pass it like parameter on the "Lets try" string. Instead of it try to exec this function apart and will work

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742360000a4429201.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信