mysql - Sequelize 存在问题,未将请求参数传递给数据库

标签 mysql node.js sequelize.js

我基本上是在尝试使用express向我的数据库发出发布请求。

我尝试分配默认值,然后它仅发布不带参数的默认值

我的数据库模型如下所示:

const Player = sequelize.define('player', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true
    },
    title: {
        type: Sequelize.STRING,
        allowNull: false,   
    },
    name: {
        type: Sequelize.STRING,
        allowNull: false
    },
    team: {
        type: Sequelize.STRING,
        allowNull: false
    },
    position: {
        type: Sequelize.STRING,
        allowNull: false,
    },
  /*   imageUrl: {
        type: Sequelize.STRING,
        allowNull: false
    }, */
    description: {
        type: Sequelize.STRING,
        allowNull: false
    },
    pickedhero1: {
        type: Sequelize.STRING,
        allowNull: false
    },
    pickedhero2: {
        type: Sequelize.STRING,
        allowNull: false
    },
    pickedhero3: {
        type: Sequelize.STRING,
        allowNull: false
    }
});

module.exports = Player;

我收到的错误是字段不能为空

notNull Violation: player.name cannot be null,
notNull Violation: player.team cannot be null,
notNull Violation: player.position cannot be null,
notNull Violation: player.description cannot be null,
notNull Violation: player.pickedhero1 cannot be null,
notNull Violation: player.pickedhero2 cannot be null,
notNull Violation: player.pickedhero3 cannot be null

即使我传递了这个查询

http://localhost:3000/api/playerz/?name=kkona&title=kkona&position=1&team=kkona&description=kkona&pickedhero1=1&pickedhero2=2&pickedhero3=3

我的路由器文件:


const express = require("express");
const Player = require('../models/player');
const router = express.Router();


router.post('/api/player/', (req, res, next) => {
    const title = req.body.title;
    const name = req.body.name;
    const team = req.body.team;
    const position = req.body.position;
    const description = req.body.description;
    const pickedhero1 = req.body.pickedhero1;
    const pickedhero2 = req.body.pickedhero2;
    const pickedhero3 = req.body.pickedhero3;
    Player.create( 
      req.body.title,
      req.body.name,
      req.body.team,
      req.body.description,
      req.body.pickedhero3,
      req.body.pickedhero2,
      req.body.pickedhero1
    ).then(result => {
        console.log(result);
    }).catch(err => {
        console.log(err);
    });
});


module.exports = router;

我的 app.js 文件如下所示:

const player = require('./routes/players');
...
app.use("/api/playerz", player);
...
sequelize.sync({force: true});

module.exports = app;

最佳答案

您应该使用req.query访问查询参数:

router.post('/api/player/', (req, res, next) => {
    const {
      title,
      name,
      team,
      position,
      description,
      pickedhero1,
      pickedhero2,
      pickedhero3
    } = req.query;

    Player.create({
      title,
      name,
      team,
      position,
      description,
      pickedhero1, 
      pickedhero2,
      pickedhero3
    }).then(result => {
        console.log(result);
    }).catch(err => {
        console.log(err);
    });
});

JFYI: But ideally you should pass all those parameters in POST body, not it a query.

关于mysql - Sequelize 存在问题,未将请求参数传递给数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57377868/

相关文章:

node.js - 尝试发布到 mysql db : "Cannot read property ' create' of undefined"时出错

node.js - 有没有办法在全局范围内编写 beforeEach ?

php - mysql查询数组的每个值

mysql - 计算列和总和

php - npm install etimed out 错误

node.js - Mongoose 脚本无法正确终止

sequelize.js - 应用迁移后 Sequelize 不更新元表

php - 使用 PHP/MySQL 的平均响应时间

mysql - MYSQL中如何通过相关表过滤具有多种特征的资源

javascript - Node.js 表达下一个错误处理程序