Paul
Paul

Reputation: 36319

Strongloop Loopback built-in model extension not being called

Not sure what I'm missing here. I am trying to extend the built-in User model in Loopback using the example in their docs: http://docs.strongloop.com/display/public/LB/Extending+built-in+models#Extendingbuilt-inmodels-ExtendingamodelinJavaScript

The file I created ./common/models/user.js is never loaded by the application, however, which of course means my extension code is never called. Any idea on the right way to do this, since the docs are wrong? Here's what I have at the moment (not that it's relevant since the file is never loaded by the framework):

console.log('User.js file being loaded');
module.exports = function(User){
  User.on('attached', function(){
    console.log(User);
  });
};

Note that neither console statement fires.

Upvotes: 0

Views: 269

Answers (1)

notbrain
notbrain

Reputation: 3396

Did you complete the model-config.json setup? There will be a User entry there already, and you'll need to add a lowercase user entry to get your extended model to load.

...
// built-in User model
"User": {
  "dataSource": "db"
},
// extends user 
"user": {
  "dataSource": "db"
}
...

You also need a user.json file that specifies that it will extend the User model that's built-in (you can find the built-in User.json and .js inside node_modules/loopback/common/models/) using "base": "User" :

{
  "name": "user",
  "base": "User",       // will include all User props
  "idInjection": true,
  "properties": {
    "pincode": {
      "type": "number"  // only add new props here
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

But I would suggest using a different name other than lowercase user to extend uppercase User since it will be easy to confuse the two. I personally have used Player and Person (with custom plural People) instead of user.

Upvotes: 2

Related Questions