我有公司和产品模型
,公司与产品模型之间有很多关系。
如何创建 Get 方法来获取公司名称
或ID
返回包含所有相关产品的公司信息。
类似于休闲对象的东西:
[{
"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"
}]
}]
最佳答案
希望您在公司模型中正确提供了关系。 如果不是就看看下面的link并创建关系。在这种情况下,关系将是 a 属于 。
Product Belongs to a Company
为了查询相关结果,您有两种方法。
- 包括公司的默认模型。因此回送的默认 get 将返回所有行。 喜欢
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'}] };
关于node.js - 如何通过环回获取主详细信息查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47479438/