无法读取Sequelize节点JS中的属性“创建”的未定义“错误

时间:2020-04-20 17:21:58

标签: javascript node.js sequelize.js

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

我的应用程序结构是:

-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;

1 个答案:

答案 0 :(得分:0)

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

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

Training.create({

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

您应该注意的另一个陷阱是,您正在create函数中分配未定义的变量,它应该是details.name, details.lastname ...