Skip to content Skip to sidebar Skip to footer

How Get All Data If Field In Query String Is Empty Node And Sequelize With Postgres

i'm studing Sequelize with Node, and i have a question.. I have the follow URL with params: http://localhost:9000/api/get/Joao/'' The first parameter is name and the second is ma

Solution 1:

You can use or statement. I think possible solution would be like below. I have not tested the code.

getCubikMembers(req, res) {
  const manager = req.params.manager;
  returnCubik
    .findAll({
      where: {
        [Op.or]: [
          {manager: manager ? manager : null },
          {name: req.params.name }
        ]
      }
    })
    .then(success => res.status(201).send(success))
    .catch(err => res.status(err).send(err))
}

Here is the docs for operators in sequelizejs.

http://docs.sequelizejs.com/manual/tutorial/models-usage.html#complex-filtering-or-not-queries

Solution 2:

i got to do, but i have that to change the "" to undefined, i dont know but the value empty is different in Sequelize..

My code was:

where: {
  name: req.params.name === 'undefined' ? {
    [Op.ne]: 'undefined'
  } : req.params.name,
  manager: req.params.manager === 'undefined' ? {
    [Op.ne]: 'undefined'
  } : req.params.manager
},

Like this if some field is undefined it is ignored in the filter..

I hope to have helped someone.

Post a Comment for "How Get All Data If Field In Query String Is Empty Node And Sequelize With Postgres"