私はlaravel(php)バックエンドと反応ネイティブのプロジェクトに取り組んでいます。私はメソッドのログイン(ユーザー名、パスワード)を使って、ユーザーを解決し、エラーを拒否する、Promoiseを返すクラス 'AuthService'を持っています。エラーはJSONで返されるので、ユーザーに特定のエラーを表示できますが、予期しないエラーが発生する可能性があるため、コンポーネントを消費するためにjsonオブジェクトも提供したいと考えています。jsonとして約束を拒否
return fetch(Config.AUTH_URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email, //provided
password: password //provided
})
})
.then(response=> {
if (response.ok) {
return response.json().then(user=> {
console.log(user);
AsyncStorage.setItem('auth.token', user.api_token);
AsyncStorage.setItem('user_name', user.user.name);
AsyncStorage.setItem('user_email', user.user.email);
})
} else {
if (response.status == 422) {
//this needs to be rejected, but gives error
return Response.reject(response.json());
} else {
//when the response from server is unexpected error (eg. 500 without json data),I still want to reject with a json object
return Promise(null,()=> {
return {"email": ["Technical problem, try later"]}
});
}
}
}, err=> {
throw Error({"email": ["Technische fout. Probeer het opnieuw"]});
});
感謝を見てみたが、APIのdoesnをフェッチ
を取得することができ拒否した場合解決コールバックを提供しない場合、応答オブジェクトのみを提供します。どうすれば拒否して解決することができますか? @Burak Karasoy –
@DesterDezzodsの回答が更新されました。あなたは解決と拒否を提供する '要求'のような別のコンポーネントを使うことができます。 –
あるいは 'fetch'を使ってエラーを投げてください。 – NiFi