简体   繁体   English

Ionic 3 HttpClient连接api时发生错误,类型“对象”上不存在属性“ json”

[英]ionic 3 HttpClient error in connecting api, Property 'json' does not exist on type 'Object'


I am using Ionic 3, I am trying to connect to a api and my code is given below. 我正在使用Ionic 3,正在尝试连接到api,下面给出了我的代码。

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map'

@Injectable()
export class WeatherProvider {
  apikey = 'xxxxxxxxxxxxxxx';
  url;
  constructor(public http: HttpClient) {
    console.log('Hello WeatherProvider Provider');
    this.url= 'http://api.wunderground.com/api/'+this.apikey+'/conditions/q';
  }
  getWeather(city, state){
    return this.http.get(this.url+'/'+state+'/'+city+'.json')
      .map(res => res.json());
  }
}

I know the error, that is with this code .map(res => res.json()); 我知道错误,这是此代码。map(res => res.json());

Here is the error log 这是错误日志

Typescript Error Property 'json' does not exist on type 'Object'. Typescript错误属性'json'在类型'Object'上不存在。

 src/providers/weather/weather.ts return this.http.get(this.url+'/'+state+'/'+city+'.json') .map(res => res.json()); } 

Ionic Framework: 3.9.2 Ionic App Scripts: 3.1.9 Angular Core: 5.2.10 Angular Compiler CLI: 5.2.10 Node: 9.2.1 OS Platform: Linux 4.13 Navigator Platform: Linux x86_64 User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 Ionic框架:3.9.2 Ionic应用程序脚本:3.1.9 Angular Core:5.2.10 Angular编译器CLI:5.2.10节点:9.2.1 OS平台:Linux 4.13 Navigator平台:Linux x86_64用户代理:Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 63.0.3239.132 Safari / 537.36

Give me a solution please 请给我一个解决方案

You do not need to call .json() with httpClient because the response is JSON by default 您不需要使用httpClient调用httpClient .json() ,因为默认情况下响应为JSON

return this.http.get(this.url+'/'+state+'/'+city+'.json')
    .map(res => res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ionic TypeScript错误(类型“ HomePage”上不存在属性“ nav”) - Ionic TypeScript Error (Property 'nav' does not exist on type 'HomePage') 类型“#”错误中不存在属性“#” - Property "#" does not exist on type "#" error ngFor 属性“id”在“对象”类型上不存在 - ngFor Property 'id' does not exist on type 'Object' “对象”类型上不存在属性“边框” - Property 'bordered' does not exist on type 'Object' 类型错误时不存在 Angular 属性 - Angular Property does not exist on type error 打字稿中的错误~类型void~中不存在属性 - Error in typescript ˜Property does not exist in type void˜ 出现“错误 TS2339:“对象”类型上不存在属性“用户”的错误。 ” - Getting error of “error TS2339: Property 'user' does not exist on type 'Object'. ” Typescript 错误“类型‘HTMLElement’上不存在属性‘值’”、“‘元素’类型上不存在属性‘onclick’” - Typescript error "Property 'value' does not exist on type 'HTMLElement'", "Property 'onclick' does not exist on type 'Element'" 类型“对象”上不存在属性“过滤器”。 尝试过滤响应时 - Property 'filter' does not exist on type 'Object'. When trying to filter response 错误TS2339:类型“ HTMLElement”上不存在属性“名称” - error TS2339: Property 'name' does not exist on type 'HTMLElement'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM