简体   繁体   中英

Sequelize: Optional limit parameter

I've seen a few questions on optional where clauses and order params, both of which I can implement, but I was wondering how to have an optional limit parameter? Maybe I'm missing something easy, but I can't see anything obvious in the docs

If I specify it like this:

const limit = req.query.limit;
Example.findAll({ 
  limit: limit
})

how can I omit limit in certain situations?

You can simply define findAll options outside findAll call and add limit prop optionally only if you have a value in req.query.limit :

const options = {
}
if (req.query.limit) {
  options.limit = req.query.limit
}
Example.findAll(options)

Or you can try to pass null if you don't ave a value in the req.query.limit (should work in PostgreSQL for instance).

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