键入&#39; Observable <void>&#39;不能分配给类型&#39; Observable <json>&#39;

时间:2017-02-17 05:02:05

标签: angular

  getStepList(): Observable<JSON>  {                  
            this.headers = new Headers();
            this.headers.append('Content-Type', 'application/json; charset=utf-8');                
            let options = new RequestOptions({
                method: RequestMethod.Post,
                url: "FeedbackMaster.aspx/getStepList",                   
                headers: this.headers,
                  body:{log:this.login}
            });
            return this._http.request(new Request(options)).retry(2)
            .map((response: Response)=>this.extractData(response))
            .do(data => {this.temp=data, console.log('getStepList:'+ JSON.stringify(JSON.parse(this.temp.d)))})
            .catch(this.handleError);                  
    }

    extractData(res: Response) {
           let body = res.json();
           return body || { }; //<--- not wrapped with data
     }
          handleError(error: any) {
         console.error('An error occurred', error);
          return Promise.reject(error.message || error);
   }  

............................................... .................................................. ......

错误

[ts]
 Type 'Observable<void>' is not assignable to type 'Observable<JSON>'.
 Type 'void' is not assignable to type 'JSON'.

我的代码有什么问题?

1 个答案:

答案 0 :(得分:4)

.map()需要返回一些内容,例如:

.map((response: Response) => { this.extractData(response); return response.json(); })

return response.json()收益Observable<JSON>。如果没有从.map()返回任何内容,则会变为Observable<void>