为什么我从 findAll Sequelize 得到未定义的电子邮件数据结果?

2022-01-19 00:00:00 node.js javascript sequelize.js

请帮忙,如何显示来自 Sequelize findAll 查询的电子邮件,因为我从我的源代码中得到未定义,有人可以帮助我吗?这是我的代码

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

这是我的屏幕截图

推荐答案

这是因为 .findAll() 方法返回的是一个数组,而不是你可以通过点符号访问的单个对象 .email.参考 .findAll() 文档.

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;
};

相关文章