gpt4 book ai didi

javascript - Sequelize - 使用多个ID而不是顺序进行查询

转载 作者:行者123 更新时间:2023-12-03 06:36:21 28 4
gpt4 key购买 nike

我有一个包含两个 id 的数组:

placeids = [22,14]

然后我有这个查询:

models.Places.findAll({
where: {
id: {in: [ placeids ]}
}

}).then(function (places) {
response(places).code(200);
}, function (rejectedPromiseError) {
response(rejectedPromiseError).code(401);
});

我希望结果按照我请求的方式返回准确的记录,在我的例子中是 22,然后是 14。

Sequelize 返回它们,但按降序排列。所以在我的例子中它返回 14 和 22。

我该如何解决这个问题?

最佳答案

您可以传递 sequelize 一个 order 参数:

models.Places.findAll({
where: {
id: {in: [placeids]}
},
order: 'id DESC'
});

或者,您可以手动订购:

.then(function (places) {
places.sort(function (x, y) {
return placeids.indexOf(x.id) > placeids.indexOf(y.id)
});
return places;
});

关于javascript - Sequelize - 使用多个ID而不是顺序进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38183193/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com