gpt4 book ai didi

javascript - "TypeError: Cannot read property ' 创建 ' of undefined"

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

我正在构建一个使用 Passport.js 处理用户的简单站点。我可以运行我的服务器,但是当用户提交他们的注册信息时,它会吐出“TypeError:无法读取未定义的属性‘创建’”。
我知道发生了什么,我就是不明白为什么。该错误在 api-routes.js 中抛出,特别是在 api/signup 函数中,当它尝试通过 db.User.create 创建用户时。当我 console.log(db) 时,db 对象中的第一件事是undefined: 'C:\\Users\\LENOVO\\Desktop\\code\\Breel\\models\\user.js'...所以用户模型正在被导出,它只是没有定义?
非常感谢您可以借给我的任何帮助!
user.js,来自我的模型目录:

var bcrypt = require("bcryptjs");

module.exports = function (sequelize, DataTypes) {
var User = sequelize.define("User", {
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true,
},
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
});
User.prototype.validPassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
User.hook("beforeCreate", function (user) {
user.password = bcrypt.hashSync(
user.password,
bcrypt.genSaltSync(10),
null
);
});
return User;
};
index.js,来自模型(this 和 user.js 是我模型目录中仅有的两个文件)
"use strict";

const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../config/config.json")[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config
);
}

fs.readdirSync(__dirname)
.filter((file) => {
return (
file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
);
})
.forEach((file) => {
const model = path.join(__dirname, file);
sequelize["import"];
db[model.name] = model;
});

Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

api-routes.js,来自我的路由目录:
var db = require("../models");
var passport = require("../config/passport");

module.exports = function (app) {
app.post("/api/login", passport.authenticate("local"), function (req, res) {
res.json("/members");
});
app.post("/api/signup", function (req, res) {
console.log(req.body);
console.log(db);
db.User.create({
email: req.body.email,
password: req.body.password,
})
.then(function () {
res.redirect(307, "/api/login");
})
.catch(function (err) {
console.log(err);
res.json(err);
});
});
app.get("/logout", function (req, res) {
req.logout();
res.redirect("/");
});
app.get("/api/user_data", function (req, res) {
if (!req.user) {
res.json({});
} else {
res.json({
email: req.user.email,
id: req.user.id,
});
}
});
};

最佳答案

您应该更正模型的注册过程:

.forEach((file) => {
const modelFile = path.join(__dirname, file);
const model = sequelize['import'](modelFile)
db[model.name] = model;
})

关于javascript - "TypeError: Cannot read property ' 创建 ' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62562791/

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