我是表达js和sequelizejs的新学习者。我成功地迁移了数据库中的表,所以我猜连接是好的。
这是我的代码。 https://github.com/Picks42/express-test 请查看此文件 https://github.com/Picks42/express-test/blob/master/models/user.js
然后回顾一下这个 https://github.com/Picks42/express-test/blob/master/controller/test.js
请告诉我出了什么问题。
最佳答案
// all the models using your index.js loader
const models = require('../models');
// the user model, note the capital User since
const M_Bank = models.User;
exports.getTest = function(req,res){
return M_Bank
.findAll()
// don't use M_Bank here since you are getting an array of Instances of the Model
.then(users => res.status(200).send(users))
.catch((error) => {
console.log(error.toString());
res.status(400).send(error)
});
/* this will never execute because it is after the return
exports.index = function (request, response, next) {
response.json((M_Bank.findAll()));
};
*/
};
如果您可以选择使用async/await
,它会使代码更具可读性。
const models = require('../models');
const M_Bank = models.User;
exports.getTest = async function(req, res) {
try {
const users = await M_Bank.findAll();
return res.status(200).send(users);
} catch (err) {
console.log(err.toString());
return res.status(400).send(err);
}
};
关于mysql - 无法读取未定义 Sequelize 的属性 'findAll',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56675122/