问题
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