如何在Strongloop回送脚手架项目中覆盖基本用户? [英] How to override base User in a Strongloop loopback scaffolded project?

查看:54
本文介绍了如何在Strongloop回送脚手架项目中覆盖基本用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个使用以下方式创建的全新项目:

Given a brand new project created with:

$ slc lb project myapp

如何用放置在./models目录中的"客户"模型替换models.json中的"用户"模型?客户应具有登录/注销等方法.用户"不应作为API存在.而且,客户模型不应是动态的.假设客户应该具有如下架构:

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:

  • 名称
  • 电子邮件
  • 密码
  • 问题
  • answer
  • 电话
  • 已验证
  • dataCreated

我已经玩了几天,而我的google-fu让我失望了.任何帮助表示赞赏.谢谢.

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

推荐答案

这是惯用的方式:

  1. models.json中,将user重命名为Customer.

models/customer.js中,向通过models.json创建的模型添加自定义方法:

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

使用strict锁定属性:

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

有关更多详细信息,请参见模型定义参考.

See Model definition reference for more details.

根据此答案下方的评论进行更新

可以先创建代码的模型,请参见

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);

基本上,这与app.bootmodels.json中的所有条目执行的代码相同.它应该向您的应用程序添加熟悉的REST路由:GET /customersPOST /customers/loginPOST /customers/logout等.

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

这篇关于如何在Strongloop回送脚手架项目中覆盖基本用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆