Francis Vagner da Luz
Francis Vagner da Luz

Reputation: 103

how to sort by ID in sequelize

class PostagemController {
        static async pegaTodasPostagens(req,res){
            try{
                const todasPostagens=await database.Postagens.findAll({order:
                        ['id','DESC']
                })
                return res.status(200).json(todasPostagens)
            }catch (error){
                return res.status(500).json(error.message)
            }
        }

I'm trying this way, but this message appears on the postman

"Unknown column 'Postagens.DESC' in 'order clause'"

Upvotes: 0

Views: 997

Answers (1)

Anatoly
Anatoly

Reputation: 22783

The order option should be an array of array in case when you wish to indicate a sorting direction otherwise Sequelize treats a flat array as an array of column names:

const todasPostagens=await database.Postagens.findAll({order:
                        [['id','DESC']]
                })

Upvotes: 2

Related Questions