假设我有下一个模型:
user.json:
{//...
"relations":{
"invoices": {
"type": "hasMany",
"model": "Invoice",
"foreignKey": "receiverId"
},
}
//...
}
又名一个用户可能有很多发票。此代码将字段 receiverId 添加到发票模型。
现在我想获取包括收款人在内的发票 list 。我该怎么做?
Invoice.find({include: "reciever"})
或者
Invoice.find({include: "user"})
无效,返回:“未为发票模型定义关系\"receiver\"”错误。
感谢您的帮助。
最佳答案
你必须定义 belongsTo发票模型中的关系。
发票.json:
{//...
"relations":{
"receiver": {
"type": "belongsTo",
"model": "Receiver"
},
}
//...
}
然后你可以像这样查询你的模型:
Invoice.find({include: "receiver"}, function(data){
console.log(data);
});
关于node.js - 有很多关系 : including from the other direction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30840146/