· articles · 2 min read
By Ankit JainHow to Fix 500 Error in JSON Server DELETE Requests
Are you getting 500 errors when using JSON-Server's DELETE call? Here is the root cause and the solution.
When using JSON Server, many developers encounter unexpected 500 errors during DELETE requests, even when deletion is successful. These errors often arise due to issues with relationships in the db.json file, particularly null foreign keys. This guide explains the root cause and offers two potential fixes.
Scenario: Why the 500 Error Occurs
The 500
error is not always straightforward. JSON Server attempts to handle data relationships for each DELETE
call. When invalid relations (e.g., null foreign keys) exist, it may still return a 500
error despite the deletion working as intended.
Example: Here’s a sample db.json
file with invalid relations:
{
"posts": [
{ "id": "1", "name": "Post 1", "photoId": "photo1" },
{ "id": "2", "name": "Post 2", "photoId": null }
],
"photos": [
{ "id": "photo1", "url": "https://abc.com/1" }
]
}
Even when deleting Post 1
, which has valid relations, you might still see a 500
error.
Solution 1: Remove Null Foreign Keys
To resolve the error, remove null foreign key properties (e.g., “photoId”: null) from the data file. This ensures that JSON Server processes only valid relationships during DELETE calls.
Downside: If your backend returns null values, removing them can make testing less realistic.
Solution 2: Use a Dummy Foreign Key Suffix
Run JSON Server with the —fks dummy parameter to bypass relation validation:
json-server db.json --fks dummy
Downside: Relations won’t be handled accurately anymore.
Conclusion
While JSON Server is a helpful mock API tool, its handling of relations during DELETE requests can be tricky. No foreign key with id
in it’s name
should be null. Adjust your data or server behavior depending on your use case to minimize errors.
For more details on the root cause you can refer here, the solution given by opami.