gpt4 book ai didi

javascript - Sequelize : TypeError: Cannot read property 'length' of undefined when try to create data on DB

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

我正在做一个个人项目。基本上是一个基于 Node 的博客来学习语言......
我已经创建了类别和文章迁移和模型,现在我正在尝试开始创建用户。
这是我的用户 迁移 (20201124222940-create_users.js):

"use strict";

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable("users", {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
},
email: {
type: Sequelize.STRING,
allowNull: false,
},
password: {
type: Sequelize.STRING,
allowNull: false,
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
},
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable("users");
},
};

这是我的 模型 (User.js):
const { Model, DataTypes } = require('sequelize');

class User extends Model {
static init(sequelize) {
super.init({
email: DataTypes.STRING,
password: DataTypes.STRING,
}, {
sequelize,
tableName: 'users'
});
}
}

module.exports = User;

//? Model de usuários
而且,这是我尝试创建用户的 Controller (UsersController.js)。
我正在使用 bcrypt 创建哈希并保护密码。
const express = require("express");
const router = express.Router();
const User = require('../models/User');
const bcrypt = require('bcryptjs');

router.get("/admin/users", (req, res) => {
res.send("Listagem de usuários");
});

router.get("/admin/users/new", (req, res) => {
res.render("admin/users/new");
});

router.post("/admin/users/create", (req, res) => {
const email = req.body.email;
const password = req.body.password;

const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(password, salt);

User.create({
email: email,
password: hash,
}).then(() => {
res.redirect("/");
}).catch((err) => {
console.log(err);
console.log(email, password, hash, salt)
});

});

module.exports = router;
我得到的错误:
TypeError: Cannot read property 'length' of undefined
at User._initValues (D:\dev\blog_node\node_modules\sequelize\lib\model.js:140:49)
at new Model (D:\dev\blog_node\node_modules\sequelize\lib\model.js:118:10)
at new User (D:\dev\blog_node\models\User.js:3:1)
at Function.build (D:\dev\blog_node\node_modules\sequelize\lib\model.js:2157:12)
at Function.create (D:\dev\blog_node\node_modules\sequelize\lib\model.js:2207:23)
at D:\dev\blog_node\controllers\UsersController.js:21:10
at Layer.handle [as handle_request] (D:\dev\blog_node\node_modules\express\lib\router\layer.js:95:5)
at next (D:\dev\blog_node\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\dev\blog_node\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\dev\blog_node\node_modules\express\lib\router\layer.js:95:5)
我已经测试了常量哈希、密码、盐、电子邮件..
已经尝试创建一些手动数据,例如:密码:“123”,电子邮件:“asda@asda.com”。
已经与其他两个模型和 Controller 进行了比较,但没有任何结果。
那么,我哪里失败了?
谢谢!
编辑
@Anatoly 注意到不要调用我的 User.init
所以......在评论之前:
const Sequelize = require("sequelize");
const dbConfig = require('./database');

const Category = require("../models/Category");
const Article = require("../models/Article");

const connection = new Sequelize(dbConfig);

Category.init(connection);
Article.init(connection);

Category.associate(connection.models);
Article.associate(connection.models);


module.exports = connection;
在他的评论之后:
const Sequelize = require("sequelize");
const dbConfig = require('./database');

const Category = require("../models/Category");
const Article = require("../models/Article");
const User = require("../models/User");

const connection = new Sequelize(dbConfig);

Category.init(connection);
Article.init(connection);
User.init(connection);

Category.associate(connection.models);
Article.associate(connection.models);


module.exports = connection;
太棒了!!! <3
解决了问题!

最佳答案

您应该在使用它之前初始化 User 模型,如下所示:

User.init(connection);
将此初始化与其他模型的初始化代码一起添加。

关于javascript - Sequelize : TypeError: Cannot read property 'length' of undefined when try to create data on DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65430643/

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