I am basically trying to make a post request to my DB using express.
I've tried assigning default values, then it posts just default values without parameters
My DB model looks like this:
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;
Error I am getting is that fields can not be null
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
even tho I am passing this query
http://localhost:3000/api/playerz/?name=kkona&title=kkona&position=1&team=kkona&description=kkona&pickedhero1=1&pickedhero2=2&pickedhero3=3
My router file:
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;
my app.js file looks like this:
const player = require('./routes/players');
...
app.use("/api/playerz", player);
...
sequelize.sync({force: true});
module.exports = app;
req.querynotreq.body