0

According to the documentation Custom Attribute Methods can be defined on the model, and then later used on objects returned from Queries made through Waterline, Sails JS's ORM.

This is not working for me. I am getting the correct data back from the query but none of functions that I declare on the model work. Trying to call them results in a TypeError: Object [object Object] has no method 'methodName'

(Update with a clearer example)

Here is my Model with the custom Attribute method

module.exports = {

attributes: {
  firstName : {
    type: 'string'
  },
  lastName : {
    type: 'string'
  }
},
  // Custom Attribute Method
  fullName : function(){
    return this.firstName + " " + this.lastName
  }
};

Here is where I am using it in the controller

module.exports = {

  findMe: function(req, res){

    User.findOne({firstName:'Todd'}).exec(function(err, user){
      console.log(user.fullName()); //<--TypeError: Object [object Object] has no method 'fullName'
      res.send(user);
    })
   }

};

What am I missing?

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
toddgeist
  • 902
  • 9
  • 21

1 Answers1

4

Full name should be included with the rest of the attributes

module.exports = {
    attributes: {
      firstName : {
        type: 'string'
      },
      lastName : {
        type: 'string'
      },
      fullName : function(){
        return this.firstName + " " + this.lastName
      }
    }
}
Meeker
  • 5,979
  • 2
  • 20
  • 38
  • we have all been there! – Meeker Sep 29 '14 at 13:39
  • I'm still there! I've my method *right there* inside of the `attributes` block -- still says its undefined. Also, I have a `toJSON` right below returning `this.toObject()` -- is there some crazy, wacky stuff happening because I'm serializing `this`? – Cody Jul 04 '15 at 05:58
  • 1
    I am not sure this is possible, did you find it on documentation? – Thomas Decaux May 18 '16 at 20:14
  • 1
    is this still working in sails 1.0? or is there any alternate to this in sails1.0? – chintan adatiya Jul 26 '19 at 13:12