typicode / json-server

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

Error with angular-cli - TypeError: Cannot read property 'id' of undefined #337

Closed ericnation closed 8 years ago

ericnation commented 8 years ago

I get this error when making a post or put request. I'm using anguar-cli in my project. Cannot read property 'id' of undefined at Function.createId (/<mypath>/web-projects/ngrx-checkout/node_modules/json-server/src/server/mixins.js:46:7) at Function.module.exports.insert (/<mypath>/web-projects/ngrx-checkout/node_modules/underscore-db/src/index.js:58:31) at /<mypath>/web-projects/ngrx-checkout/node_modules/lodash/lodash.js:3899:28 at arrayReduce (/<mypath>/web-projects/ngrx-checkout/node_modules/lodash/lodash.js:618:21) at baseWrapperValue (/<mypath>/web-projects/ngrx-checkout/node_modules/lodash/lodash.js:3898:14) at LodashWrapper.wrapperValue (/<mypath>/web-projects/ngrx-checkout/node_modules/lodash/lodash.js:8345:14) at LodashWrapper.<anonymous> (/<mypath>/web-projects/ngrx-checkout/node_modules/lowdb/lib/_index.js:77:19) at apply (/<mypath>/web-projects/ngrx-checkout/node_modules/lodash/lodash.js:409:27) at LodashWrapper.wrapper [as value] (/<mypath>/web-projects/ngrx-checkout/node_modules/lodash/lodash.js:4837:16) at create (/<mypath>/web-projects/ngrx-checkout/node_modules/json-server/src/server/router/plural.js:210:8)

wuzhouyang commented 8 years ago

HI,do you resolve it?

ericnation commented 8 years ago

Its been so long I actually can't remember how I resolved it. Sorry.

typicode commented 8 years ago

@wuzhouyang In case you have the same issue, one of the possible reasons can be that in db.json you have an array with no id.

For example, this will fail:

{ "posts": [ { "title": "..." } ] }

But this will work:

{ "posts": [ { "id": 1, "title": "..." } ] }
doremidom commented 7 years ago

You might want to explicitly state the id requirement for db.json to the documentation. I had the same issue, it was quite difficult to debug without knowing that the db had to be formatted a specific way.

jemshit commented 7 years ago

Why force id field?

architggg commented 6 years ago

simple solution. to this is adding "id" to your JSON object. I just did that and it is working fine.

TechGeekD commented 6 years ago

what if you just pass --id or -i to switch id to another key field

CreativeCoder28 commented 4 years ago

JSON data needs a property named "id" to store the data we send.(If already some data is stored manually in the DB. So if you already have data in your json file please make sure that u add "id" property to it. e.g. "data": [ { "id": "1", "name" : "Andy",