const pEntries = await DB.models.pcPhoto.findAll({
raw : true,
attributes : ['filename'],
where: {
eid : { $in: await DB.models.entrySettings.findAll({
attributes : ['id'],
raw: true,
where: {
emplId : '99999'
}
})
}
}
});
我从子查询中得到以下结果
[ {id: 801},{id: 802},.....{id:900} ]
执行查询时,错误是
UnhandledPromiseRejectionWarning:错误:无效值{id:968}
如何处理此问题并获取唯一的值数组,以便查询能够顺利执行。
答案 0 :(得分:0)
const settings = await DB.models.entrySettings.findAll({
attributes: ['id'],
raw: true,
where: { emplId : '99999' }
})
const eIds = settings.map(el => el.id)
const pEntries = await DB.models.pcPhoto.findAll({
raw : true,
attributes : ['filename'],
where: {eid: {
$in: eIds
}}
}
答案 1 :(得分:0)
您可以在单个查询中执行此操作,而无需使用子查询,但是为此您需要定义适当的 association 黑白模型,例如方式
pcPhoto.hasMany(entrySettings , { foreignKey: 'eid' });
然后运行:
DB.models.pcPhoto.findAll({
raw : true,
attributes : [ 'id' , 'filename' , 'eid' ],
include : {
model : DB.models.entrySettings ,
where : { emplId : '99999' }
}
});