简体   繁体   中英

Sequelize - Query with multipleID and not order

I have an array of two ids:

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.

Sequelize return them, but it orders them in descending. So in my case it returns 14 and 22.

How can I address this?

You can pass sequelize an order parameter:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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