TypeError User.findOne不是函数

时间:2019-05-15 03:58:14

标签: node.js express sequelize.js

根据文档和在线教程使用它,我调用用户模型的方式有问题吗?

users.js

//User model
const User  = require('../models/User');

//Validation passed
        User.findOne({ email: email})
        .then(user => {
            if(user) {
                //user exists throw errors
            } else {
                res.send('create new user entry')
            }
        });

我的User.js模型

module.exports = function(sequelize, Sequelize) {

    var User = sequelize.define('user', {
    });
    return User;
}

1 个答案:

答案 0 :(得分:0)

从此更改用户模型文件

module.exports = function(sequelize, Sequelize) {

    var User = sequelize.define('user', {
    });
    return User;
}

像这样

const Sequelize = require("sequelize");
const sequelize = require("../util/connection");
const User = sequelize.define("user", {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  fname: {
    type: Sequelize.STRING
  },
  lname: {
    type: Sequelize.STRING
  }
});


module.exports = User;

问题在于用户模型文件,您没有正确导入用户模型。 否则尝试这样的事情。

//User model
const Usermodel  = require('../models/User');
const User = Usermodel(sequelize,Sequelize);

//Validation passed
        User.findOne({ email: email})
        .then(user => {
            if(user) {
                //user exists throw errors
            } else {
                res.send('create new user entry')
            }
        });

您能否发布index.js文件