Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server error 500 on POST #798

Closed
casvil opened this issue Jun 8, 2018 · 7 comments
Closed

Server error 500 on POST #798

casvil opened this issue Jun 8, 2018 · 7 comments

Comments

@casvil
Copy link

casvil commented Jun 8, 2018

Hi, I am having issues with POST method in my React-Redux app. GET works perfectly fine.

Error in Chrome devtools: POST https://my-json-server.typicode.com/casvil/recipes/recipes 500 (). Uncaught (in promise) SyntaxError: Unexpected token T in JSON at position 0

Error in Postman: TypeError: Cannot read property 'id' of undefined
at Function.createId (/app/node_modules/json-server/lib/server/mixins.js:46:39)
at Function.insert (/app/node_modules/lodash-id/src/index.js:47:49)
at /app/node_modules/lodash/lodash.js:4379:28
at arrayReduce (/app/node_modules/lodash/lodash.js:704:21)
at baseWrapperValue (/app/node_modules/lodash/lodash.js:4378:14)
at LodashWrapper.wrapperValue (/app/node_modules/lodash/lodash.js:9067:14)
at create (/app/node_modules/json-server/lib/server/router/plural.js:226:50)
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
at next (/app/node_modules/express/lib/router/route.js:137:13)
at next (/app/node_modules/express/lib/router/route.js:131:14)

This is the fetch request:

export const createRecipe = (recipeData) => dispatch => {
    fetch('https://my-json-server.typicode.com/casvil/recipes/recipes', {
      method: 'POST',
      headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(recipeData)
  })
    .then(res => res.json())
    .then(recipe => dispatch({
      type: NEW_RECIPE,
      payload: recipe
    }));
}

db.json:

{
  "recipes": [
    {
      "name": "Tortillicas fatas",
      "difficulty": "2/5"
    },
    {
      "name": "Croquetas de pollo",
      "difficulty": "3/5"
    }
  ]
}

If I use instead your Posts json-server, the POST method works fine. https://jsonplaceholder.typicode.com/posts.

Is it possible that my db.json file is wrong, might have any wrong character?

Thanks!

@casvil
Copy link
Author

casvil commented Jun 8, 2018

The db.json file needs an "id" property

@casvil casvil closed this as completed Jun 8, 2018
@Itainatan
Copy link

thank you!!

@raboija
Copy link

raboija commented Aug 5, 2019

Thank you

@DakotaLMartinez
Copy link

just wanted to clarify here that the ids are auto-assigned on post requests sent to json-server, but any data that you've added to db.json yourself needs to have an id property with an integer value.

@vdrr89
Copy link

vdrr89 commented Jun 24, 2021

I had the same problem, I added an "id" and it worked! thank you! but anyway I don't understand why it needs an "id" ((I'm new with this))

@BohdanSolo
Copy link

The db.json file needs an "id" property

thank you!

@PhamHuuPhat
Copy link

thank you ❤️❤️❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants