Json-server: 500 errors on DELETE

Created on 27 Jun 2017  路  15Comments  路  Source: typicode/json-server

I can GET/PUT/POST to my db, but when I try DELETE I get a 500 response. Funny thing is the record is actually deleted. I have a very simple db (no nesting, no custom routes). Just a basic install.

Most helpful comment

For those who haven't found a reason for the DELETE 500 here is one more scenario.

I had relations in my data and json-server will try to handle invalid relations in each DELETE call. Hence a data file with invalid relations in the DB for any of the models under processing suffices to cause the 500 error. Below is one examples and then solutions how to fix the issue

db.json (invalid photoId: null for post with id: "2")

{
  "posts": [
    {
      "id": "1",
      "name": "Post 1",
      "comment": "Comment 1",
      "photoId": "photo1"
    },
    {
      "id": "2",
      "name": "Post 2",
      "comment": "Comment 2",
      "photoId": null
    }
  ],
  "photos": [
    {
      "id": "photo1",
      "url": "https://abc.com/1"
    }
  ]
}

DELETE /posts/1

  • notice how we are deleting post with id: "1" which does not have any incorrect relations and still we will receive the error 500

Fix 1

  • Remove the photoId: null property from the post with id: "2"

Downside is that if your actual backend returns null values and those are somehow required in the receiving end then this makes testing harder. Unless you add a custom express wrapper that will populate the null fields in the response without actually affecting the db file.

Fix 2

  • Pass a dummy foreign key suffix parameter for json-server

json-server db.json --fks dummy

Downside is that relations are not handled correctly anymore.

All 15 comments

Strange, would you have some stack trace or reproducible example?

Thx for responding @typicode, Here's the request/response info...

screen shot 2017-06-29 at 5 50 00 pm

...and the stack trace:

TypeError: Cannot read property 'toString' of null
    at /Users/brian/dev/vault-fe/node_modules/lodash-id/src/index.js:37:50
    at baseFindIndex (/Users/brian/dev/vault-fe/node_modules/lodash/lodash.js:823:11)
    at findIndex (/Users/brian/dev/vault-fe/node_modules/lodash/lodash.js:7275:14)
    at Function.find (/Users/brian/dev/vault-fe/node_modules/lodash/lodash.js:5098:21)
    at Function.getById (/Users/brian/dev/vault-fe/node_modules/lodash-id/src/index.js:35:17)
    at /Users/brian/dev/vault-fe/node_modules/json-server/lib/server/mixins.js:25:25
    at /Users/brian/dev/vault-fe/node_modules/lodash/lodash.js:4944:15
    at baseForOwn (/Users/brian/dev/vault-fe/node_modules/lodash/lodash.js:3001:24)
    at /Users/brian/dev/vault-fe/node_modules/lodash/lodash.js:4913:18
    at Function.forEach (/Users/brian/dev/vault-fe/node_modules/lodash/lodash.js:9359:14)
DELETE /users/rk4MoGJV- 500 14.189 ms - 876

Any updates on this? I'm experiencing the exact same issue.

Thank you!

Follow-up: Delete did work for us, but our json file has grown dramatically.

Today, I removed all of the JSON except for one entity and delete worked fine. So, there's something wrong with my db.json file.

If possible, it would be great if the json-server code could give context in the error. I'm unable to see what part of the JSON it's failing on. I'm not sure if this is possible.

same issue here, when I make my db.json smaller, it works like a charm, when the db.json is little bit bigger then I have the error.

  • just noticed that when I DELETE it have been deleted virtually but not from db.json (POST works like a charm with db.json when adding data)

trace:
{ opts: { foreignKeySuffix: 'Id' } }
TypeError: Cannot read property 'toString' of null
at \node_modules\lodash-id\src\index.js:37:50
at baseFindIndex (\node_modules\lodash\lodash.js:823:11)
at findIndex (\node_modules\lodash\lodash.js:7275:14)
at Function.find (\node_modules\lodash\lodash.js:5098:21)
at Function.getById (\node_modules\lodash-id\src\index.js:35:17)
at \node_modules\json-server\lib\server\mixins.js:26:25
at \node_modules\lodash\lodash.js:4944:15
at baseForOwn (\node_modules\lodash\lodash.js:3001:24)
at \node_modules\lodash\lodash.js:4913:18
at Function.forEach (\node_modules\lodash\lodash.js:9359:14)
DELETE /tests/1 500 14.180 ms - -

For those who haven't found a reason for the DELETE 500 here is one more scenario.

I had relations in my data and json-server will try to handle invalid relations in each DELETE call. Hence a data file with invalid relations in the DB for any of the models under processing suffices to cause the 500 error. Below is one examples and then solutions how to fix the issue

db.json (invalid photoId: null for post with id: "2")

{
  "posts": [
    {
      "id": "1",
      "name": "Post 1",
      "comment": "Comment 1",
      "photoId": "photo1"
    },
    {
      "id": "2",
      "name": "Post 2",
      "comment": "Comment 2",
      "photoId": null
    }
  ],
  "photos": [
    {
      "id": "photo1",
      "url": "https://abc.com/1"
    }
  ]
}

DELETE /posts/1

  • notice how we are deleting post with id: "1" which does not have any incorrect relations and still we will receive the error 500

Fix 1

  • Remove the photoId: null property from the post with id: "2"

Downside is that if your actual backend returns null values and those are somehow required in the receiving end then this makes testing harder. Unless you add a custom express wrapper that will populate the null fields in the response without actually affecting the db file.

Fix 2

  • Pass a dummy foreign key suffix parameter for json-server

json-server db.json --fks dummy

Downside is that relations are not handled correctly anymore.

Is it possible to pass the foreign key suffix parameter in case of using the json-server as a module?
( node test.js )

Yes, it seems to be an undocumented feature to pass the foreign key suffix as second parameter to the module. Below code sets it to dummy

let data = require('data.json)
const jsonServer = require("json-server");
const server = jsonServer.create();
const router = jsonServer.router(data, "dummy");

Thanks a lot! That works perfect!

This solution solved deletion but it seems to break expansion. Any calls using ?_expand fail for me when this suffix is set

Hmm... I tested the JSON Placeholder API for deleting their data. The result is an empty object if the process was successful. Because of the current limitation, I just passed the id by using the querystring and requested using DELETE method.

Here's the screenshot, and the result is {}. This is not the solution, but it just a quick trick to save your time.

temp_solution_error_500

The downside:

  • Data is not deleted.
  • Pardon me... Actually, this a 404 request, but the response is an empty object...

Hi Folks
Love the product. Presently though, I am getting the "DELETE /OutboxShopItems/4 500 18.383 ms - 1000" error message on version 0.14.2 of the server.
The json file is question is 4K in size not large by any standard. Is there an issue raised to address this size limitation on DELETE operations?

Thanks

Hi. Is there any resolution to this? Setting the foreign key isn't working for and the only thing that seems to work is to have less data, which isn't really a solution.

Loving this package, but am a bit stumped about this one. Also, when it does work, I would probably expect it to return the data row without the id set (or set to 0).

Thanks.

My solution was that there was an unrelated table to the one I was deleting from that didn't have the id on the every row (user error in the json schema setup before json server starts, id wasn't set as "required").

So is there something going on in the lowDB methods that is trying to do related data deletes?

Would still be nice if the delete method returned the row deleted though.

Thanks anyway

Well, I had the exact same error and thanx to @opami's post, I debugged the whole thing and came to a conclusion that no foreign key with id in it's name should be null. The check throws an error when it finds one, so I then set it to -1 and now it workes ok.

This problem basically annoys anyone having a zero or many or zero or one relations in their tables, since it doesn't return any results on deletion. At "node_modules/json-server/lib/server/mixins.js:22:9" (v0.16.0) a check for null in _value_ variable besides the foreign key, would be enough.

EDIT: Since you cannot query ids by null, it is prudent not to use it as a key value

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boydenhartog picture boydenhartog  路  3Comments

zenith77 picture zenith77  路  3Comments

pantchox picture pantchox  路  3Comments

melnikovic picture melnikovic  路  3Comments

ashleydavis picture ashleydavis  路  3Comments