json-server icon indicating copy to clipboard operation
json-server copied to clipboard

DELETE is working but throwing an error when using a custom id key

Open jellehak opened this issue 3 years ago • 1 comments

When setting a different id key with:

router.db._.id = '_id'

Or with:

NODE_OPTIONS=--stack-trace-limit=100 json-server storage/db.json --port 5000 --watch --id _id

If you do a DELETE request it will throw an error.

Code of interest: https://github.com/typicode/json-server/blob/master/src/server/router/plural.js#L318

Stack trace

TypeError: Cannot read property 'toString' of undefined
    at /usr/local/lib/node_modules/json-server/node_modules/lodash-id/src/index.js:37:51
    at baseFindIndex (/usr/local/lib/node_modules/json-server/node_modules/lodash/lodash.js:816:11)
    at findIndex (/usr/local/lib/node_modules/json-server/node_modules/lodash/lodash.js:7322:14)
    at Function.find (/usr/local/lib/node_modules/json-server/node_modules/lodash/lodash.js:5121:21)
    at Function.getById (/usr/local/lib/node_modules/json-server/node_modules/lodash-id/src/index.js:35:17)
    at Function.removeById (/usr/local/lib/node_modules/json-server/node_modules/lodash-id/src/index.js:110:20)
    at /usr/local/lib/node_modules/json-server/node_modules/lodash/lodash.js:4430:28
    at arrayReduce (/usr/local/lib/node_modules/json-server/node_modules/lodash/lodash.js:697:21)
    at baseWrapperValue (/usr/local/lib/node_modules/json-server/node_modules/lodash/lodash.js:4429:14)
    at LodashWrapper.wrapperValue (/usr/local/lib/node_modules/json-server/node_modules/lodash/lodash.js:9114:14)
    at /usr/local/lib/node_modules/json-server/lib/server/router/plural.js:290:47
    at Array.forEach (<anonymous>)
    at destroy (/usr/local/lib/node_modules/json-server/lib/server/router/plural.js:289:17)
    at Layer.handle [as handle_request] (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/layer.js:95:5)
    at next (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/route.js:137:13)
    at next (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/route.js:131:14)
    at next (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/route.js:131:14)

Related to #279

jellehak avatar Apr 14 '22 08:04 jellehak

I can confirm the issue, It go on error also with preceding the --id before the db.json

json-server --id _id --watch db.json --routes json-server-routes.json --port 3333

clabnet avatar Jul 18 '22 12:07 clabnet