Why I get Undefined email data result from findAll Sequelize?

丶灬走出姿态 提交于 2021-02-05 12:28:17

问题


Please help, how to show email from Sequelize findAll query, because i get Undefined from my source code, can some body help me ? Here my code

testdata.get = (req, res) => {
  User.findAll()
    .then(data => {
      console.log(data.email);
    })
    .catch(err => {
      res.json({
        message: (err, "Error")
      });
    });
  return;
};

My result on terminal like this

Executing (default): SELECT `id`, `email`, `password`, `level`, `createdAt`, `updatedAt` FROM `Users` AS `User`;
undefined

There's my screenshoot


回答1:


It is because .findAll() method returns an array, not a single object which you can access by the dot notation .email. Refer to .findAll() docs.

testdata.get = (req, res) => {
  User.findAll()
    .then(data => {

        data.forEach((object) => console.log(object.email));

    })
    .catch(err => {
       res.json({
       message: (err, "Error")
    });
  });
  return;
};



回答2:


I think the data will be an array of objects and not just an object.

So I suggest you log data first and then loop through that and check if it contains the email key.



来源:https://stackoverflow.com/questions/48258708/why-i-get-undefined-email-data-result-from-findall-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!