如何通过环回获取主明细查询?

How to get a master detail query by loopback?

我有公司和产品 model 从公司到产品模型有很多关系。 如何使用 Get 方法获取 company nameID returns 公司信息以及所有相关产品。

类似于休闲对象的东西:

 [{
        "C_name": "tatli",
        "address": "TR-IS- aktepe",
        "tel": "123456789",
        "id": "5a18772e61b6370e4c713b44",
      [{
    "P_Code": "123456",
    "P_name": "screw",
    "QTY": 30,
    "id": "5a1878af61b6370e4c713b46",
    "compny_id": "5a18772e61b6370e4c713b44"
  },
  {
    "P_Code": "123457",
    "P_name": "Bead",
    "QTY": 33,
    "id": "5a1878af61b6370e4c713b47",
    "compny_id": "5a18772e61b6370e4c713b44"
  }]
    }]

希望您已在 Company 模型中正确提供关系。 如果不只是看看下面的 link 和 Create the relation 。在这种情况下,关系将是一个 belongs to 。

Product Belongs to a Company

查询相关结果有两种方式。

  • 将其包含在 Company 的默认模型中。所以 loopback 的默认 get 将 return 你所有的行。 喜欢

company.json模型文件

  "relations": { // make sure the name of product model and foreign key is correct
    "product": {
      "type": "belongsTo",
      "model": "product",
      "foreignKey": "productId"
    }
  },

 "scope": {
    "include": "product"
  },
  • 第二种方式是写在远程方法中。
Company.getPrefs = function(id, cb) {
    Company.find({
    where: {        
    },
    include: [{relation: 'Product'}]
};