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

2

I have the following problem

  

[ts] The 'json' property does not exist in the 'Object' type. 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);
    });
  });
 }
    
asked by ninomory 30.12.2017 в 06:39
source

1 answer

2

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

HttpClient Angular official 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);
        }
      );
    });
  }
    
answered by 17.01.2018 в 20:00