需要帮助在sequelize

时间:2016-05-09 15:27:27

标签: mysql node.js express sequelize.js

现在我创建了一个有效的函数,它在当前时间之前获取所有消息。但问题是,它首先是最早的消息,而不是我想做的事情。

使用sequelize会解决此问题的最佳方法是什么?

module.exports.getMessages = function(chat_name, ammount, callback){
        Message.findAll({
            limit: ammount,
            where: {
                chat: chat_name,
                createdAt:{
                    $lt: new Date(),
                }
            }
        }).then(function(dates){
            return callback(null, dates)
        }).catch(function(err){
            return callback(err, null);
        })
    }

1 个答案:

答案 0 :(得分:0)

您需要将order属性添加到传递给findAll的对象中。 order: [['createdAt', 'DESC']]

module.exports.getMessages = function(chat_name, ammount, callback){
        Message.findAll({
            limit: ammount,
            where: {
                chat: chat_name,
                createdAt:{
                    $lt: new Date(),
                }
            },
            order: [['createdAt', 'DESC']]
        }).then(function(dates){
            return callback(null, dates)
        }).catch(function(err){
            return callback(err, null);
        })
    }

更多http://docs.sequelizejs.com/en/latest/docs/querying/#ordering

相关问题