2016-03-19 9 views
-1

私の返答が返され、未定義として印刷される理由について質問があります。インポートされたクラスを使用してES6で空の約束が返される

これは私のmain.jsの私の方法です。私は未定義の応答を取得していますどこ

find() { 
     return this.serviceFactory 
      .createRequest() 
      .withId(333) 
      .sendAsGet().then(response => { 
       console.log(response); 
      }); 
    } 

上記にconsole.log(レスポンス)があります。

これはparseJSONContent方法でthis.serviceFactory

export class ServiceFactory { 
    constructor(http) { 
     this.http = http; 
     this.http.baseUrl = serviceUrlParts.baseUrl; 
     this.endpoint = ""; 
     this.id = ""; 
    } 

    createRequest() { 
     return this; 
    } 

    withId(id) { 
     this.id = id; 
     return this; 
    } 

    setEndPoint(endpoint){ 
     this.endpoint = serviceUrlParts.baseUrl + endpoint; 
    } 

    sendAsGet() { 
     return this.http.get(`${this.endpoint}/${this.id}`) 
      .then(response => { 
       this.parseJSONContent(response); 
      }).catch(error => { 
       console.log(error); 
      }); 
    } 

    parseJSONContent(response) { 
     console.dir(JSON.parse(response.content)); 
     return JSON.parse(response.content); 
    } 
} 

はconsole.dir(JSON.parse(response.content))として使用されるインポートクラスが正しいオブジェクトを印刷されAPIから返されました。どこかに戻ってコールに戻り、失われています。何が起こっているのかについてのいくつかの洞察は非常に高く評価されるでしょう!

.then(response => { 
    this.parseJSONContent(response); 
}) 

変更それに:ここで

+1

をので、あなた*やる*リターン 'undefined'(または、ドンこれらのコールバックすべてから何も返さない)。 – Bergi

答えて

2

あなたは値が返さない

.then(response => { 
    return this.parseJSONContent(response); 
}) 

または

.then(response => this.parseJSONContent(response)) 
+0

ああ、あなたに感謝します。あなたが投稿した "or"オプションを使って行ったが、両方とも正しく動作していた。できるだけ早く答えとしてマークします。 – Shawn

関連する問題