如何在Loopback中获取嵌套的master-detail或master-detail -detail查询

时间:2018-01-20 06:17:52

标签: javascript node.js mongodb loopbackjs loopback

我有categorycategory_subs,主要明细模型,帖子模型belongs to category_subs。在下面的代码中,我可以获得两者的主要细节,但我不知道如何将post包含到它们甚至attachment模型中的远程方法的帖子。

module.exports = function (Category) {
    Category.categorySubs = function (id, cb) {
    Category.find({
        where: {
          id: id
        },    
        include: {
            relation: 'categorySubs',
          scope: {
              include: 'category_subs'
          }
        }
      },
      function (err, posts) {
        cb(null, posts);
      });
  }
 Category.remoteMethod('categorySubs', {
    accepts: {
      arg: 'id',
      type: 'string'
    },
    returns: {
      arg: 'ID',
      type: 'string'
    },
    http: {
      path: '/iteminfo',
      verb: 'get'
    }
  });

更新

category.json

"relations": {
    "categorySubs": {
      "type": "hasMany",
      "model": "category_subs",
      "foreignKey": "catgory_id"
    }
  },

category_subs

"relations": {
    "posts": {
      "type": "hasMany",
      "model": "post",
      "foreignKey": "category_sub_id"
    }
  },

1 个答案:

答案 0 :(得分:1)

我用谷歌搜索,我认为你必须深入了解https://loopback.io/doc/en/lb3/Include-filter.html