Noah Tony
Noah Tony

Reputation: 383

json-server getting error after post request

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

Upvotes: 4

Views: 11509

Answers (3)

Ajinkya Shewale
Ajinkya Shewale

Reputation: 1

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

Upvotes: 0

Kwadwo Frimpong
Kwadwo Frimpong

Reputation: 1

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

Upvotes: 0

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.

Upvotes: 17

Related Questions