4

I am working with json-server and I am getting following error. What I am doing wrong?

TypeError: Cannot read property 'id' of undefined [0] at Function.createId (/Users/Picchu/Documents/url/node_modules/json-server/lib/server/mixins.js:47:39) [0] at Function.insert (/Users/Picchu/Documents/url/node_modules/lodash-id/src/index.js:47:49) [0] at /Users/Picchu/Documents/url/node_modules/lodash/lodash.js:4388:28 [0] at arrayReduce (/Users/Picchu/Documents/url/node_modules/lodash/lodash.js:683:21) [0] at baseWrapperValue (/Users/Picchu/Documents/url/node_modules/lodash/lodash.js:4387:14)

  createShortUrl(data: ShortUrl): Observable<any> {
    let params = new HttpParams();
    params = params.append('url', 'http://google.com');
    return this._http.post(`${'/api'}`, { params: params }).pipe(map((res) => {
      return res;
  }
Noah Tony
  • 373
  • 3
  • 7
  • 18

3 Answers3

15

If you have some data already in the JSON DB (before we start sending requests), make sure those objects have a property named "id".

For eg:

{

cards:[

"id":"1",
      "name":"something"

   ]

}

A JSON data needs a property named "id" to store the data we send.(If already some data is stored manually in the DB)

If the DB is empty when we send our first request, it will assign a parameter "id" automatically(and give it some random value) in addition to the provided data (if we don't mention an "id" parameter explicitly) and for every further request.

0

instead of making a "post" request change it to a "get" request

0

The json-server first property should be "id" : 1

Popmedic
  • 1,791
  • 1
  • 16
  • 21