Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
  }
like image 934
Noah Tony Avatar asked Sep 12 '25 18:09

Noah Tony


1 Answers

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.

like image 197
Praveen Balasubramaniam Avatar answered Sep 15 '25 17:09

Praveen Balasubramaniam