javascript - 如何修复使用 Sequelize Node Express Mysql 插入数据库的问题?

标签 javascript mysql node.js express sequelize.js

我正在尝试创建一个简单的注册表单,然后我将继续执行 Node js 新的其他 CRUD 操作

我已经创建了 mysql 数据库并完成了建模和与 Sequelize 的连接,我还使用 pug 创建了我的 View ,并且我为表单中的条目做了 console.log ,一切都很好,直到它到达数据库插入

var express = require('express');
var router = express.Router();
var Sequelize = require("sequelize");
var db = require('../src/database/connection');
var User = require('../src/model/User');

router.get('/', function(req, res, next){
  res.render('signup');
});
router.post('/register', function(req, res, next){
    let { username, useremail, pass, re_pass } = req.body;

    let errors = [];

    //checking feilds 
    if( !username || !useremail || !pass || !re_pass)
    {
        errors.push({msg: 'Please no field should be left empty'});
    }
    //checking Password
    if(pass !== re_pass)
    {
        errors.push({msg: 'Passwords Dont Match'});
    }
    //check Password Length
    if(pass.length < 7)
    {
        errors.push({msg: 'Passwords should be more than 7 characters '})
    }

    //reload page with contents if error encountered
    if(errors.length > 0)
    {
        res.render('signup', {
            errors,
            username,
            useremail,
            pass,
            re_pass
        });
    } else{
        console.log(req.body);
        User.create({
        username,
        useremail,
        pass,
        }).then(user => res.redirect('/register')).catch(err => console.log(err));
    }
});
module.exports = router;

这是我收到的错误。请帮忙

{ SequelizeValidationError: notNull Violation: User.email cannot be null,
notNull Violation: User.password cannot be null
    at Promise.all.then (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\sequelize\lib\instance-validator.js:74:15)
    at tryCatcher (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise.js:694:18)
    at Promise._fulfill (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise.js:638:18)
    at PromiseArray._resolve (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise_array.js:126:19)
    at PromiseArray._promiseFulfilled (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise_array.js:144:14)
    at Promise._settlePromise (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise.js:574:26)
    at Promise._settlePromise0 (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\promise.js:694:18)
    at _drainQueueStep (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\async.js:138:12)
    at _drainQueue (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\async.js:131:9)
    at Async._drainQueues (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\ONLINEWIS-PROG-03\Documents\Node\waiyu\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
  name: 'SequelizeValidationError',
  errors:
   [ ValidationErrorItem {
       message: 'User.email cannot be null',
       type: 'notNull Violation',
       path: 'email',
       value: null,
       origin: 'CORE',
       instance: [User],
       validatorKey: 'is_null',
       validatorName: null,
       validatorArgs: [] },
     ValidationErrorItem {
       message: 'User.password cannot be null',
       type: 'notNull Violation',
       path: 'password',
       value: null,
       origin: 'CORE',
       instance: [User],
       validatorKey: 'is_null',
       validatorName: null,
       validatorArgs: [] } ] }

最佳答案

似乎req.body始终为空 使用body parser将请求正文放入 req.body

const express = require('express'),
      app = express(),
      bodyParser = require('body-parser');

// support parsing of application/json type post data
app.use(bodyParser.json());

//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));

关于javascript - 如何修复使用 Sequelize Node Express Mysql 插入数据库的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56166883/

相关文章:

javascript - 如何从 jQuery Ajax 访问 JSON 元素

mysql - 我怎样才能得到这个查询中的逾期天数?

php - 执行mysqli准备的stmt SELECT * FROM table来获取没有bind_params的所有行,可能吗?

node.js 模块从命令行运行,而不是从脚本运行

windows - 从 node.js 启动一个外部应用程序

node.js - 如何使用 2048 位的 Diffie-Hellman 来创建密码?

javascript - 用于响应式设计的具有不同文件的 HTML5 视频

javascript - 如何在数组方法(如过滤器)中使用 break 语句?

javascript - Jquery 获取 A 标签的closest() Iframe 返回未定义

javascript - 使用来自多个其他行的值创建一个表行