Sequelize -使用multipleID而不是顺序进行查询

Sequelize - Query with multipleID and not order

本文关键字:顺序 查询 使用 multipleID Sequelize      更新时间:2023-09-26

我有一个两个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。

我该如何解决这个问题?

可以传递sequelizeorder参数:

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;
});