node.js - Strongloop:如何使用代码(不是 REST API)获取相关模型

标签 node.js strongloop loopback

User 对象上获取相关模型时遇到问题。 用户客户具有一对多关系。

我不能只说 User.customers 来获取与 User 关联的 customers 吗?

我已经尝试过

User.find({include:'customers'}, function(err, user) {

   //now what?

  //user.customers does not work; there is no Customer array returned.



});

很高兴在文档中查找此内容,但我找不到这是写在哪里的。

谢谢

最佳答案

在回送示例中,他们经常创建“用户”模型作为回送“用户”模型的扩展。

注意小写的 u。

当使用“User”而不是“user”时,我无法访问模型

用户.json

{
 "name": "user",
 "base": "User",
 "idInjection": true,
 "emailVerificationRequired": false,
 "properties": {
 "createdAt": {
   "type": "date"
 },
 "updatedAt": {
  "type": "date"
 },
 .......

用户.js

module.exports = function(user) {


user.observe('before save', function(ctx, next){
  if (ctx.instance) {
  //If created at is defined
  if(ctx.instance.createdAt){
    ctx.instance.updatedAt = new Date();
  }
  else{
    ctx.instance.createdAt = ctx.instance.updatedAt = new Date();
  }
} else {
  ctx.data.updatedAt = new Date();
}
next();
})`

关于node.js - Strongloop:如何使用代码(不是 REST API)获取相关模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30559259/

相关文章:

node.js - 无法在 Gitlab.com 上的共享运行器上以 headless 模式运行 Puppeteer

javascript - 如何使用 AJAX 执行 Pug for 循环?

javascript - 如何在终端中比较 Chrome 和 NodeJS 之间的 V8 Javascript 引擎版本?

javascript - 如何设置 ACL 以允许每个人列出来自 REST API 的所有用户

html - 使用 Hogan.js 进行部分处理

node.js - Strongloop 在 afterRemote 方法中创建相关模型对象

javascript - 双向模型引用策略

javascript - Loopback自定义远程方法错误回调

node.js - NestJs vs Loopback 4

javascript - 如何将包含图像文件的流发送到环回中的远程方法?