我想将另一个表导入到我的结果中,但是我有一个小问题。以下是我的控制器,对重要的地方进行了评论。
问题在于NULL值where: { id: transactions.id_sender }
我认为我必须使用.map或for循环,但是我想知道,什么是解决此问题以及可能答案的好方法。
exports.getRecipientdata = (req, res) => {
const userId = req.params.recipientId;
Transaction.findAll({
where: {
id_recipient: userId,
},
}).then(transactions => {
console.log('\n transactions', `${JSON.stringify(transactions)}\n`);
Transaction.findAll({
where: { id_sender: userId },
attributes: [
'amount_money',
'date_time',
'transfer_title',
'id_recipient',
'id_sender',
],
include: [
{
model: User,
where: { id: transactions.id_sender }, // <- this is need a results with: '2', '2', '3';
attributes: ['name', 'surname'],
},
],
})
.then(transactionsWithName => {
console.log(
'\n transactionsWithName',
`${JSON.stringify(transactionsWithName)}\n`,
);
res.send(transactionsWithName);
})
.catch(err => {
console.log(err);
});
});
};
交易JSON:
[
{
"id": 1,
"id_sender": 2,
"id_recipient": 1,
"date_time": "2019-01-19T20:09:30.000Z",
"amount_money": 7,
"transfer_title": "payment1"
},
{
"id": 4,
"id_sender": 2,
"id_recipient": 1,
"date_time": "2019-01-19T20:23:33.000Z",
"amount_money": 8.4,
"transfer_title": "payment2"
},
{
"id": 11,
"id_sender": 3,
"id_recipient": 1,
"date_time": "2019-01-20T10:34:29.000Z",
"amount_money": 16.5,
"transfer_title": "payment3"
}
]
transactionsWithName JSON:
[]