我基本上是在尝试使用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;
我的应用.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:但理想情况下,您应该在 POST 正文中传递所有这些参数,而不是查询。
- 您正在使用查询而不是正文的用户(请注意:您应该使用 body 进行 POST(
-
创建时,应按如下方式创建:
router.post('/api/player/', (req, res, next) => { const title = req.query.title; const name = req.query.name; const team = req.query.team; const position = req.query.position; const description = req.query.description; const pickedhero1 = req.query.pickedhero1; const pickedhero2 = req.query.pickedhero2; const pickedhero3 = req.query.pickedhero3; Player.create( { title: title, name: name, team: team, position: position, description: description, pickedhero1: pickedhero1, pickedhero2: pickedhero2, pickedhero3: pickedhero3 }).then(result => { console.log(result); }).catch(err => { console.log(err); }); });