Billing
和 Pspecial
具有 1:m
关系。所以,
Billing.hasMany(Pspecial, {as: 'details',foreignKey: 'billing_id'})
现在,我想获取给定帐单的几个详细信息之一,并确保帐单存在于第一位。
GET/api/billings/:billingId/details/:pspecialId
async function getParticular(req, res, next) {
try {
let billing;
let result;
billing = await Billing.findById(req.params.billingId);
if(!billing) {
throw new Error('Billing Not found');
}
result = await billing.getParticular(req.params.particularId);
//Oh, wait, but billing.getParticular is not a function
if(!result) {
throw new Error('Particular Not found');
}
res.json(result);
} catch (e) {
next(e)
}
}
我该如何获取众多细节之一?
最佳答案
您正在寻找的方法是getParticulars
,它根据传递的 options
where
属性返回相关模型实例的列表
result = await billing.getParticulars({ where: { id: req.params.particularId } });
result
将是一个包含 id = 1
的详细信息数组,因此可能是单个元素数组。
关于javascript - Sequelize hasMany 关系的 getAssociation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903105/