【问题标题】:ERROR: User is not a constructor -- node js with express , sequelize, postgresSQL错误:用户不是构造函数——带有 express、sequelize、postgresQL 的节点 js
【发布时间】:2021-03-14 04:01:50
【问题描述】:

我正在使用 node.js(sequalize,express) 和 postgresql db 制作登录应用程序,在插入数据时我收到 ERROR:TypeError: User is not a constructor

//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() 方法后,我得到了错误。

【问题讨论】:

  • module.exports = User 我猜。
  • @TGrif 不适用于 module.exports = User

标签: node.js express sequelize.js


【解决方案1】:
const user = await User.create(userParam); no need `new` operate

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 2020-11-24
    • 2017-03-22
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多