1

I have some difficulty understanding how Strongloop models behave. There is a lot of documentation about static and remote methods, but how about general class methods?

Let's say I have a user model, that has a method for showing the full name:

module.exports = function (User) {
    User.name = function () {
        return User.firstname + ' ' + User.lastname;
    }
};

How do I fetch this user and use the method? I would suppose:

var User = app.models.User;

User.findById('559103d66d', function (err, model) {
    console.log(model.name());
});

But apparently, the findById returns a JSON object containing all the properties instead of the actual model. So how does one define and use model methods in Strongloop?

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
Arne
  • 6,140
  • 2
  • 21
  • 20

1 Answers1

1

You need to use 'prototype' property of javascript, if your are planning to use name() function on the instance of 'User' model. As follows:

User.prototype.name = function () {
   return this.firstname + ' ' + this.lastname;
}

and you are good to go.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
xangy
  • 1,185
  • 1
  • 8
  • 19