gpt4 book ai didi

node.js - 错误 : User is not a constructor -- node js with express , Sequelize ,postgresQL

转载 作者:行者123 更新时间:2023-12-03 22:17:25 25 4
gpt4 key购买 nike

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

//用户/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
}

//用户/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() 方法后,我收到错误消息。

最佳答案

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

关于node.js - 错误 : User is not a constructor -- node js with express , Sequelize ,postgresQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54334483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com