typicode / json-server

Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Other
72.66k stars 7.01k forks source link

500 errors on DELETE #568

Closed uberspeck closed 1 month ago

uberspeck commented 7 years ago

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.

typicode commented 7 years ago

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

uberspeck commented 7 years ago

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
schmiderific commented 7 years ago

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

Thank you!

schmiderific commented 7 years ago

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.

ReDragon710 commented 6 years ago

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.

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 - -

opami commented 6 years ago

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

Fix 1

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

json-server db.json --fks dummy

Downside is that relations are not handled correctly anymore.

tonyuifalean commented 6 years ago

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

opami commented 6 years ago

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");
tonyuifalean commented 6 years ago

Thanks a lot! That works perfect!

GxDesign commented 6 years ago

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

jeffryang24 commented 6 years ago

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:

ennisa-ire commented 5 years ago

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

McGern commented 3 years ago

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.

McGern commented 3 years ago

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

Rock-son commented 3 years ago

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

PuzzleDotsCode commented 1 month ago

Hack fix: use Nodemon to watch db.json

    "dev:json-server": "nodemon --watch db.json --exec \"json-server db.json --port 3001\"",
    "dev": "concurrently \"npm run dev:next\" \"npm run dev:json-server\"",
    "dev:next": "next dev",
typicode commented 1 month ago

JSON Server automatically watches db.json so there's no need for nodemon