【问题标题】:Loopback: How to include a relationship results in a queryLoopback:如何在查询中包含关系结果
【发布时间】:2015-10-18 20:39:18
【问题描述】:

我有两个模型:NewsItem 和 user。每个 NewsItem 都属于一个用户。

"relations": {
"user": {
  "type": "belongsTo",
  "model": "user",
  "foreignKey": "publisherId"
}
}

关系已保存在数据库中,因此可以正常工作。

我无法列出所有带有关联用户名称的 NewsItem。

 app.models.NewsItem.find({
    include: [
        {
        relation: 'user',
        scope: {
          fields: ['name']
        }
      }
      ]
  },  function(err, newsItems) { ...

根据文档 (https://docs.strongloop.com/display/public/LB/Include+filter),NewsItem.user 将指向关系方法而不是用户实例。

有人知道我如何访问用户名值吗?

【问题讨论】:

    标签: loopbackjs strongloop


    【解决方案1】:

    我发现了问题,我需要调用NewsItem.user()来获取对象实例。

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 2015-02-27
      • 2017-12-11
      • 2016-11-25
      • 2018-07-01
      • 2019-03-26
      相关资源
      最近更新 更多