Receipt of findAll() in the nodejs

Asked

Viewed 75 times

-3

I’m having a huge doubt here.

router.post('/login', (req,res)=>{
  Usuario.findAll({where: {email: req.body.email}}).then((x)=>{
   console.log(x.email)
  })
})

Result of 'console.log(x.email)' is Undefined. if I do the console.log(x) the result is as in the picture below. I need to have x.email content to perform a comparison.

Thank you very much in advance.

inserir a descrição da imagem aqui

  • 3

    x is an array, not an object. You must iterate over the array elements to access them as objects.

1 answer

2


The problem is that the attribute x is not an object but an array. To get the value of the property email you will need to access the first index of the array that will save an object.

After that, just access the property dataValues and thus obtain the email, thus:

router.post('/login', (req, res) => {
    Usuario.findAll({where: {email: req.body.email}}).then((x) => {
       console.log(x[0].dataValues.email);
    });
});
  • really. I don’t know where my head was. thanks! = ) it worked.

Browser other questions tagged

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