续集 findOne 最新条目

Sequelize findOne latest entry

我需要在 table 中找到最新条目。做这个的最好方式是什么? table 具有 Sequelize 的默认 createdAt 字段。

方法 findOnefindAll 方法的包装器。因此,您可以简单地使用 findAll 和 limit 1 并按 id 降序排列。

示例:

YourModel.findAll({
  limit: 1,
  where: {
    //your where conditions, or without them if you need ANY entry
  },
  order: [ [ 'createdAt', 'DESC' ]]
}).then(function(entries){
  //only difference is that you get users list limited to 1
  //entries[0]
}); 
model.findOne({
    where: { key },
    order: [ [ 'createdAt', 'DESC' ]],
});

如果有人将时间戳设为假,你也可以用 id 来做。这个对我有用。

model.findOne({
order: [ [ 'id', 'DESC' ]],
});