簡體   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