gpt4 book ai didi

javascript - 无法读取 Sequelize Node js 中未定义的属性 'create'”错误

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

我正在使用 node、sequelize、migration 和 postgre。我已经在“models”文件夹的文件中定义了我的模型,但是每次我尝试运行我的端点时,我都会收到错误消息
“无法读取未定义的属性‘创建’”

我的应用程序结构是:

-bin
www
-config
config.js
-models
training.js
-public
-routes
-views

下面是我的代码:
我的模型(training.js)
'use strict'

module.exports = (Sequelize, DataTypes) => {

const Training = Sequelize.define('Training', {


id: {
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},

firstname: {
type: DataTypes.STRING,
notEmpty: true
},

lastname: {
type: DataTypes.STRING,
notEmpty: true
},

email: {
type: DataTypes.STRING,
validate: {
isEmail: true}
},

gender: {
type: DataTypes.STRING,

},
course: {
type: DataTypes.STRING,
},

address: {
type: DataTypes.STRING,

},

registered: {
type: DataTypes.DATE
},
number : {
type: DataTypes.STRING,
allowNull: false
}


}, {});
return Training;

}




和我的路线文件:
var express = require('express');
var router = express.Router();
const bodyParser = require('body-parser');
const { check, validationResult } = require('express-validator');
const Sequelize = require('sequelize');
const db = require('.././config/config');
const Training = require('.././models').Training;



/* GET home page. */
router.get('/', function(req, res, next) {
res.render('training', { title: 'Register' });
});

router.post('/',[
check('name').isLength({ min: 1 }).withMessage('First is required').trim().escape(),

check('lastname').isLength({ min: 1 }).withMessage('Lastname is required').trim().escape(),

check('email').isEmail().withMessage('That email doesn‘t look right').bail().trim().normalizeEmail(),

check('number').isInt().withMessage('Number is Invalid'),

check('address').isLength({ min: 1 }).withMessage('adddress is required').trim().escape()


], function(req, res, next) {

const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).jsonp(errors.array());
} else {

let details = {
name : req.body.name,
lastname : req.body.lastname,
email : req.body.email,
number :req.body.number,
gender :req.body.gender,
course :req.body.course,
address : req.body.address

}


db.Training.create({

firstname : name,
lastname : lastname,
email : email,
gender :gender,
course : course,
address : address
})
.then(console.log('saved succesfully'))
.catch(err => console.error(err.message))



}

});





module.exports = router;

最佳答案

您不需要配置来访问模型。不需要导入 const db = require('.././config/config');

由于您已使用此行导入 Trainingconst Training = require('.././models').Training; ,您可以继续使用模型,如下所示:

Training.create({

firstname : name,
lastname : lastname,
email : email,
gender :gender,
course : course,
address : address
})

您应该注意的另一个问题是您在 create 函数中分配了 undefined variable ,它应该是 details.name, details.lastname ...

关于javascript - 无法读取 Sequelize Node js 中未定义的属性 'create'”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61328318/

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