2016-07-04 40 views
0

我有兩個ID的數組:Sequelize - 查詢與multipleID而不是命令

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.

我該如何解決這個問題?

+0

我懷疑Sequelize他們的訂單,它的SQL數據庫這樣做。能否影響該順序取決於數據庫是否支持這樣的事情(MySQL確實,請參閱[本答案](http://stackoverflow.com/a/396771/893780))。 – robertklep

回答

1

您可以通過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; 
});