错误:用户不是构造函数-具有express,sequelize和postgresSQL的节点js

时间:2019-01-23 19:37:42

标签: node.js express sequelize.js

我正在使用node.js(sequalize,express)和postgresql db制作登录应用程序,同时插入数据时出现错误:TypeError:用户不是构造函数

// users / user.model.js

module.exports = (sequelize, type)=> {
    return sequelize.define('user', {
        id: {
            type: type.INTEGER,
            primaryKey: true,
            autoIncrement: true
          },
        username:  type.STRING,
        hash: type.STRING,
        firstName: type.STRING,
        lastName: type.STRING,
    },{ freezeTableName: true })
} 

// __ helper / db.js

const config = require('config.json');
const Sequelize = require('sequelize');
const UserModel = require('../users/user.model');

//const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');
//var sequelize = new Sequelize(config.database, config.username, config.password);
const sequelize = new Sequelize(config.connectionString1);
const User = UserModel(sequelize, Sequelize)

module.exports = {
  User
}

// users / user.service.js

const { User } = require('../_helpers/db');
sync function create(userParam) {
    const user = new User(userParam);

    // save user
    await user.save();
}

在user.service中调用save()方法后,出现错误。

1 个答案:

答案 0 :(得分:0)

const user = await User.create(userParam); no need `new` operate