0

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;
1
  • 1
    You're passing an empty body and all values as a query-params. Those values are available via req.query not req.body Commented Aug 6, 2019 at 14:14

2 Answers 2

1

You should use req.query to access query params:

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Yea, I tried but now I can not even post, I tried setting up body in postman aswell, didnt help, must have something with validation, reading the docs now
Which content-type are you trying to POST (e.g. application/json, multipart/form-data, etc)? In case of json you need to add body-parser middleware before using router. express.json
try app.use(...) instead of app.subscribe(...)
still the same, gonna check now with more options
I fixed it with some kind of sorcery I read in sequelize documents, it was validation stuff, thanks either way ♥
0
  1. user you are using query not body.( just a note: you should use body for POST)
  2. when you create you should create like below:

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

3 Comments

still the same error, I even set body in postman, it's the same, I think it has something to do with validation
the above code only works with the query request from your question. console log title before create and see if you can actually get value ?
I fixed it with some kind of sorcery I read in sequelize documents, it was validation stuff, thanks either way ♥

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.