Prabir Choudhury
Prabir Choudhury

Reputation: 236

Unable to send request to MongoDB nodeJS ADD through POSTMAN -

The question is pretty much self explanatory. I have created a backend for MongoDB using nodeJS. Here is the link to my entire backend project so you can replicate at your own end -->

https://onedrive.live.com/?cid=90b44987ff0f70ac&id=90B44987FF0F70AC%21157&authkey=!AOG8D2nRT--ga9I

Now, through Postman I am trying to send requests to the add operation to add new player items - The steps I am following are -->

  1. starting up mongo.exe and running use playerDB
  2. starting up mongod.exe
  3. opening command terminal on my backend project and running nodemon server
  4. sending a POST request to Mongoose db through postman like localhost:4000/playerDB/add

The error I am receiving is -> 400 Unable to add player object

My sample request body is like so:

{
    "player_name":"Didier Drogba",
    "player_description":"Brawny, cunning, determined and a heart for attempting the most impossible audacious! The one and only!",
    "player_position":"Striker",
    "player_age":"42",
    "player_club":"Chelsea FC",
    "player_":"Hall of Fame",
    "player_isactive":"False",
    "player_completed":"True"
}

What error did I make? What was the problem? Anythign wrong with the sequence of steps I followed? Can you point out where I went wrong?

Thanks,

EDIT--> The new error is:

<body>
        <pre>SyntaxError: Unexpected token F in JSON at position 288
            <br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)
            <br> &nbsp; &nbsp;at parse (E:\REACT\MyProj\backend\node_modules\body-parser\lib\types\json.js:89:19)
            <br> &nbsp; &nbsp;at E:\REACT\MyProj\backend\node_modules\body-parser\lib\read.js:121:18
            <br> &nbsp; &nbsp;at invokeCallback (E:\REACT\MyProj\backend\node_modules\raw-body\index.js:224:16) .....

Upvotes: 0

Views: 45

Answers (1)

Sunny Parekh
Sunny Parekh

Reputation: 983

I have seen your code and there were a couple of changes that were needed.

1) In your player.model.js file, the module.exports should be at the end of the file. This is because you are exporting the player schema even before it is declared and defined.

2) Two properties in the model: a) player_isactive and b) player_completed are defined as Boolean and you were passing a string from the postman. So you need to send the data as follows:

{
    "player_name":"Didier Drogba",
    "player_description":"Brawny, cunning, determined and a heart for attempting the most impossible audacious! The one and only!",
    "player_position":"Striker",
    "player_age":"42",
    "player_club":"Chelsea FC",
    "player_":"Hall of Fame",
    "player_isactive": false,
    "player_completed":true
}

Kindly make this changes and your problem is solved.

Upvotes: 1

Related Questions