mysql - 无法读取未定义 Sequelize 的属性 'findAll'

标签 mysql node.js express sequelize.js

我是表达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/

相关文章:

javascript - 如何将路由器放入路由器内部

python - 为什么列的默认值不适用于 django

mysql - 关于Mysql主从配置

node.js - Firebase 3.3.x Nodejs - createUserWithEmailAndPassword 不是函数

node.js - Parent.setChild 不是一个函数

mysql - 书架级联删除

mysql - HIVE 中的 FIRST() 或 LAST() 聚合函数

Python 导入 MySQLdb 错误 - Mac 10.6

node.js - 为什么 fastify-cli 生成的应用程序与文档中的示例有如此不同?

node.js - 将查询数据从函数返回到 james/express.js 页面