我可能会遗漏一些显而易见的东西,但却花费了数小时才能找到解决方案。
假设我有一个员工模型
"properties": {
"name": {
"type": "string",
"required": true
},
"age": {
"type": "number",
"required": true
}
};
及其子模型
"properties": {
"Code": {
"type": "string",
"required": true
},
"Desc": {
"type": "string",
"required": true
}
}
如何在它们之间创建多对多关系?
答案 0 :(得分:0)
您可以添加将接受字段传递给remoteMethod选项的已接受属性。 请在documentation。
中阅读此页面module.exports = function(Task) {
fs.readdir(PATH_PROCESS+PATH_API, (err, o) => {
for(var c in o){
var i = o[c];
Task[i] = require(PATH_PROCESS+i+".js").lpb;
Task.remoteMethod(
i, {
http: {path: ('/'+i), verb: 'post'},
returns: {arg: 'result', type: 'object'},
accepts: [
{arg: 'params', type: 'object', required: true, http: {source: 'body'},
description: (i+' params.')}
]
}
);
}
});
};
答案 1 :(得分:0)
来自docs:
hasManyThrough关系与另一个模型建立多对多连接。此关系表示通过第三个模型可以将声明模型与另一个模型的零个或多个实例匹配。
使用slc loopback:relation命令创建模型之间的关系。不要忘记在提示时添加模型(也在文档中解释)。
在模型之间创建关系后,您必须使用automigrate()或autoupdate()将更改与数据库同步。
使用automigrate时要小心,因为它会创建或重新创建数据库,这意味着您可能会丢失数据。