我有一个 Sequelize 查询形成的对象列表,类似于这样:
[
Likes {
dataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' },
_previousDataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' },
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
},
Likes {
dataValues: { PlaceId: '417f55a6-1d64-491f-9c00-8f3094f3f53a' },
_previousDataValues: { PlaceId: '417f55a6-1d64-491f-9c00-8f3094f3f53a' },
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes: [Array]
},
isNewRecord: false
}
]
当我查询时,我会得到以下两种列表之一,导致出现各种异常:[{PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd'},{PlaceId: 'e678yt6-c846-46b5-aa25-6534d67326au'}] 或 [].
我已经尝试了Object.values、valueOf、map和其他内置方法,甚至手动使用for循环,但我真的不明白为什么它们不起作用。你有什么解决方法建议吗?
解决方案:
return models.Likes.findAll({
where: {
UserId: req.user.id
},
attributes: ['PlaceId']
}).then(likes => {
var values;
try {
values = likes.map(entity => entity.get('PlaceId'))
}
catch (e){
console.log(e);
}
models.Place.findAll({
where: {
id: {
[Sequelize.Op.notIn]: values
}
},
limit: 24
})
这是最终的工作解决方案,感谢其中一位建议者提供的指导。使用 (entity => entity.get) 进行映射即可解决问题。向所有评论和帮助过我的人致以最好的问候。
如果有人需要,这个东西会从一个表执行 sequelize 查询,然后再查询另一个带有“NOT IN”的表。在 SQL 查询中,它看起来像这样:
"SELECT "id", "coordinates", "place_name", "description", "category", "createdAt", "updatedAt" FROM "Places" AS "Place" WHERE "Place"."id" NOT IN ('e61d2360-c846-46b5-aa25-6534d38276dd', '417f55a6-1d64-491f-9c00-8f3094f3f53a') LIMIT 24;"
[{"placeid":"xxx"},{"placeid":"zzz"}]
是一个对象数组。这个:Likes { dataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' }, _previousDataValues: { PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd' }
不是。 - mplungjanI get either a list like that: [{PlaceId: 'e61d2360-c846-46b5-aa25-6534d38276dd'}, {PlaceId: 'e678yt6-c846-46b5-aa25-6534d67326au'}] or an empty list: [].
so I expecteddataValues: [ here is an array ]
- mplungjan