Is the use of the operator "and" in this code correct? Because it is not working

Asked

Viewed 45 times

0

The function of this code is to receive the login data through the req and do with query to see if the records exist and the user can access certain area, but the code below not be indifferent as much as type the correct data in the form, how much type the wrong data in the form.

routeradmin.post('/envlogin', function (req, res) {
  dadoslogin.findAll({
    where: {
      email: req.body
         senha: {
        [Op.and]: req.body.senhauser
      }

    }, raw: "true"
  }).then(
    function () {
      res.send("deu certo");
    }).catch(function (error) {
      res.send("deu errado");
    });
});

As the query in my vision would return the value or not, I used .then and .cacth to process the chances of outcome.

1 answer

1


Apparently you want to make a query that results in AND for verification of senha and email, that is, the SQL query you expect would be algorithm of type:

SELECT * FROM post WHERE email = '[email protected]' AND senha = '123';

In that case we should do the following:

routeradmin.post('/envlogin', function (req, res) {
  dadoslogin.findAll({
    where: {
    [Op.and]: [ // usamos o "Op.and" dessa forma
      { email: req.body },
      { senha: req.body.senhauser }
    ]
  }, raw: "true"
  }).then(
    function () {
      res.send("deu certo");
    }).catch(function (error) {
      res.send("deu errado");
    });
});

that way Sequelize inferred that we want a AND for both checks (email and senha), this generates:

SELECT * FROM post WHERE (email = '[email protected]') AND (senha = '123');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.