2016-08-19 19 views
1
getVerificationCode(phoneNumber) { 
     let url = `${remote}/auth/phoneNumber/${phoneNumber}`; 
     return this._http.get(url) 
      .toPromise() 
      .then(result => { 
       console.log(result.body); <--- ERROR 
      }); 
     } 

上記のコードでは、結果が返されるとbodyというプロパティがあります。 typescriptですコンパイラは次のエラーをスローしかし:これに対処する方法はAngular2/Typescriptコンパイルエラー:プロパティ 'body'がタイプ 'Response'に存在しません。

Error TS2339: Property 'body' does not exist on type 'Response'. 

わからない静か。

私はタイプ

interface ResultData<T> { 
    body: string 
} 

のインタフェースを定義しようとしたし、次にとしてそれを使用して:

this.getVerificationCode('1111111') { 
    let url = `${remote}/auth/phoneNumber/${phoneNumber}`; 
    return this._http.get(url) 
      .toPromise() 
      .then((result: ResultData<any>)=> { 
        console.log(result.body); <--- ERROR 
       }); 

が、その後、私は次のようなエラーになるだろう:

Error TS2345: Argument of type '(result: ResultData<any>) => void' is not assignable to parameter of type '(value: Response) => void | PromiseLike<void>'. 
    Types of parameters 'result' and 'value' are incompatible. 
    Type 'Response' is not assignable to type 'ResultData<any>'. 
     Property 'body' is missing in type 'Response'. 

答えて

0

はそれを試しを

getVerificationCode(phoneNumber) { 
    let url = `${remote}/auth/phoneNumber/${phoneNumber}`; 
    return this._http.get(url) 
     .toPromise() 
     .then((result: Object) => { 
      console.log(result.body); <--- ERROR 
     }); 
    } 
関連する問題