2016-11-04 1 views
1

私はこのようなフェッチ要求を持っています。Fetch Apiから返されるPromiseオブジェクト内でエラー値を取得するにはどうすればよいですか?

fetch(deafaultUrl + '/v1/users/', 
     { 
      headers: { 
       'Content-Type': 'application/json' 
      }, 
      method: "POST", 
      body: JSON.stringify(userInfoParams) 
     }) 
     .then(function(response) { 
      console.log(response); 
      console.log(response.json()); 

      // not working never go into this function 
      response.json().then(function(value) { 
       console.log('access promise'); 
       console.log(value); // "Success" 
      }); 

      if (response.status !== 200 && response.status !== 201) { 
       throw new Error("Bad response from server"); 
      } 
     }) 
     .then(function(json){ 

      console.log("succeed json re"); 
      console.log(json); 
      dispatch(update_user(json)); 

     }) 

だから私はこのconsole.log(response.json());

を慰めるとき、私はこの

enter image description here

を得たが、約束の機能は、ワークアウトをdoesnotのように思えます。

[[PromiseValue]]の中にエラーオブジェクトを取得するにはどうすればよいですか?

ありがとうございます!ここで

+0

なぜダブル 'then'ですか? – Weedoze

答えて

1

あなたはこのようにそれを使用する必要がありますthen(...)

のドキュメントです:

p.then(function(value) { 
    // fulfillment 
    }, function(reason) { 
    // rejection 
}); 

次に、あなたのコードは、この

fetch(deafaultUrl + '/v1/users/', { 
     headers: { 
      'Content-Type': 'application/json' 
     }, 
     method: "POST", 
     body: JSON.stringify(userInfoParams) 
    }) 
    .then(function(response) { 
     //Land here if request is successful 
     console.log(response); 

     if (response.status !== 200 && response.status !== 201) { 
      throw new Error("Bad response from server"); 
     } 
    }, function(error){ 
     //Land here if request has an error 
     console.log(error); 
    }); 
+0

これはまだ動作していません。エラー422を受けても、2番目のブロックではなく最初のブロックに入ります。 – user3403614

+0

'422'エラーを確認してください。エラーがあなたの電話にあります – Weedoze

0

のようになります。私はresponse.json()ができるだけで解決策を見つけました私がconsole.log(response.json());の場合は一度読むとうまくいきます。

+0

これは正しいです。複数回読む場合は、レスポンス(https://developer.mozilla.org/en-US/docs/Web/API/Response/clone)をクローンする必要があります。 Btw、それはそれを記録する方法ではありません。 console.log(response.json())は、json()関数によって返された約束事を記録しますが、結果をログに記録します(response.json()。then(data => console.log(data))。 – Marco

2

約束のエラーを得たために、あなたはまた、(.catch使用することができます約束のエラーを得たために、この

promise.then(function(data) { 
    }, function(error) {...} 

OR

のように行うことができますが)

.then(){...} 
.catch(){..} 

をブロックしたが、これに行くことはありませんサーバーが既に応答を送信したために一部

.then(function(response) { 
      console.log(response); 
      console.log(response.json()); 

      // not working never go into this function 
      response.json().then(function(value) { 
       console.log('access promise'); 
       console.log(value); // "Success" 
      }); 

あなたはこのようにしてコードを書くことができます:

fetch(deafaultUrl + '/v1/users/', { 
     headers: { 
      'Content-Type': 'application/json' 
     }, 
     method: "POST", 
     body: JSON.stringify(userInfoParams) 
    }) 
    .then(function(response) { 
     // if request is successful 
    } 
    .catch(err){ 
     // if got error 
    } 
    }); 
関連する問題