简体   繁体   English

Sequelize-查询带有multiID而不是顺序

[英]Sequelize - Query with multipleID and not order

I have an array of two ids: 我有两个ID的数组:

placeids = [22,14]

Then I have this query: 然后我有这个查询:

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

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

I want the result to return exact records, the way I have requested them, in my case 22 and then 14. 我希望结果以我要求的方式返回确切的记录,在我的情况下是22,然后是14。

Sequelize return them, but it orders them in descending. Sequelize将它们返回,但是按降序排列它们。 So in my case it returns 14 and 22. 因此,在我的情况下,它返回14和22。

How can I address this? 我该如何解决?

You can pass sequelize an order parameter: 您可以传递sequelize order参数:

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

Or, you can do the ordering manually: 或者,您可以手动进行订购:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM