jsonplaceholder icon indicating copy to clipboard operation
jsonplaceholder copied to clipboard

Sending PUT /todos/201 on a created todo with id 201 returns 500

Open tommywalkie opened this issue 6 years ago • 4 comments

Hi, When I edit a todo resource and send a PUT /todos/200 request, it works and returns the updated fake JSON data.

{
  "id": 200,
  "title": "edited title",
  "userId": 10,
  "completed": true
}

When I create a todo resource with POST /todos, it worked and I could get a todo with id 201.

{
  "title": "test",
  "userId": 1,
  "completed": false,
  "id": 201
}

But I try to edit the todo resource and send a PUT /todos/201 request with these body :

{
  "title": "edited test todo title",
  "userId": 1,
  "completed": false,
  "id": 201
}

... It returns an 500 Internal Server Error.

TypeError: Cannot read property 'id' of undefined
    at update (/app/node_modules/json-server/lib/server/router/plural.js:258:24)
    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)
    at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
    at /app/node_modules/express/lib/router/index.js:281:22
    at param (/app/node_modules/express/lib/router/index.js:354:14)
    at param (/app/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/app/node_modules/express/lib/router/index.js:410:3)

Note : This error doesn't occur when I delete this todo resource with DELETE /todos/201 Looks like a json-server specific problem, but just to be sure, am I doing something wrong ?

tommywalkie avatar Jun 10 '19 17:06 tommywalkie

@tommywalkie any news here ? I'm getting the same error as you on both PUT and DELETE requests for newly created todos.

EnricoMassone avatar Feb 12 '20 20:02 EnricoMassone

@EnricoMassoneDeltatre Originally, I was using this tool as part of an employment test, I'm not using it anymore so I have no idea about it. That said, I didn't have this error on DELETE requests.

tommywalkie avatar Feb 13 '20 08:02 tommywalkie

@tommywalkie It says in the website

Important: resource will not be really updated on the server but it will be faked as if.

So I think what you are experiencing is expected. When you sent the post req, it fakes as if a new entry is created for you but it doesn't in actual. So the put req you're sending for the newly created entry returns an error since there isn't any such entry created in the first place.

What's happening with delete seems to be an issue.

s-rishi avatar Oct 12 '21 06:10 s-rishi

@s-rishi The point is if "it will be faked as if" then we shouldn't expect an internal server error.

tommywalkie avatar Oct 14 '21 12:10 tommywalkie