繁体   English   中英

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

[英]Sequelize - Query with multipleID and not order

我有两个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;
});

暂无
暂无

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

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