最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to override base User in a Strongloop loopback scaffolded project? - Stack Overflow

programmeradmin1浏览0评论

Given a brand new project created with:

$ slc lb project myapp

How do I replace the 'user' model in models.json with a 'customer' model placed in the ./models directory? Customers should have the login/logout etc. methods and 'users' should not exist as an API. Also, the customer model should not be dynamic. Let's pretend customer should have a schema as follows:

  • name
  • email
  • password
  • question
  • answer
  • phone
  • verified
  • dataCreated

I've been playing with this for a couple of days and my google-fu is letting me down. Any help appreciated. Thanks.

Given a brand new project created with:

$ slc lb project myapp

How do I replace the 'user' model in models.json with a 'customer' model placed in the ./models directory? Customers should have the login/logout etc. methods and 'users' should not exist as an API. Also, the customer model should not be dynamic. Let's pretend customer should have a schema as follows:

  • name
  • email
  • password
  • question
  • answer
  • phone
  • verified
  • dataCreated

I've been playing with this for a couple of days and my google-fu is letting me down. Any help appreciated. Thanks.

Share Improve this question edited Sep 30, 2014 at 13:55 Deduplicator 45.8k7 gold badges72 silver badges123 bronze badges asked Jul 1, 2014 at 16:33 huxleyhuxley 2951 gold badge4 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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 ment 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".

发布评论

评论列表(0)

  1. 暂无评论