How to override base User in a Strongloop loopback scaffolded project?

我是研究僧i 提交于 2019-12-03 17:23:19

This is the idiomatic way:

  1. In models.json, rename user to Customer.

  2. In models/customer.js, add custom methods to the model created via models.json:

    var app = require('../app');
    var Customer = app.models.Customer;
    
    Customer.myfn = function(cb) {
      // etc.
      cb();
    };
    

Also, the customer model should not be dynamic. Let's pretend customer should have a schema as follows

Use strict to lock down the properties:

{
  "Customer": {
    "options": {
      "base": "User",
      "strict": true
    },
    "properties": {
      // define properties (schema)
    }
  }
}

See Model definition reference for more details.

Update based on the comment below this answer

It is possible to create your models code-first, see customer.js in LoopBack's sample app that was built before models.json was introduced.

Here is what you should do in your code:

var app = require('../app');

var Customer = module.exports = loopback.createModel(
  'Customer',
  {
    name: 'string',
    // and all other more properties
  },
  {
    base: 'User',
    strict: true
  }
);

app.model(Customer);

That is basically the same code that is executed by app.boot for all entries in models.json. It should add the familiar REST routes to your application: GET /customers, POST /customers/login, POST /customers/logout, etc.

It is very difficult to help you without seeing your code that does not work and knowing what exactly do you mean by "not working".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!