2 votes

The property 'json' does not exist in the type 'Object'.

I have the following problem

ts] Property 'json' does not exist on type 'Object'. any

This is the code:

postData(credentials,type){
  return new Promise((resolve, reject) =>{
    let headers = new HttpHeaders();
    this.http.post(apiUrl+type, JSON.stringify(credentials), {headers: headers}).
    subscribe(res =>{
      resolve(res.json());
    }, (err) =>{
      reject(err);
    });
  });
 }

0 votes

before the subscribe put .map(res => res.json()). ... and remove the .json() from your implementation.

3voto

Steven Pardo Points 396

If you are using the HttpClient it is not necessary to convert to JSON, since the response of the observable is already a JSON.

Official HttpClient Angular documentation

for this reason the code should look like this:

  public postData(credentials: Object, type: string): Promise<any> {
    return new Promise((resolve, reject) => {
      this.http.post(apiUrl + type, credentials).subscribe(
        response => {
          resolve(response);
        },
        exception => {
          reject(exception);
        }
      );
    });
  }

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X