Reactjs isomorphic-fetch PATCH怎么有身体错误?

时间:2016-05-26 09:35:21

标签: javascript reactjs xmlhttprequest

所以基本上我使用fetch POST或PATCH方法发送数据,当我有错误时,我可以在网络中看到 - >回复此错误:

{
  "Errors": [
   {
     "Code": -201,
     "Message": "Could not get file from link",
     "AssociatedError": "404 Not Found"
   }
 ],
 "Result": null
}

这是我的实际代码:

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  } else {
    var error = new Error(response.statusText);
    error.response = response;
    throw error;
  }
}

export function sendImageUploaded(data, valueMethod, endpoint) {
    return dispatch => {
        dispatch(requestPosts(data));
        return fetch(endpoint, {
            method: valueMethod,
            headers: new Headers({
                            Authorization: Isengard.config.token
                        }),
            body: data
        })
        .then(checkStatus)
        .then(reponse => {
            dispatch(successSent("The output list has been successfully sent!"));
        }).catch(err => {
            console.log('request failed', err);
            dispatch(failSent("Error on sending request: " + err));
        });
    };
};

我正在努力解决此错误消息。

1 个答案:

答案 0 :(得分:2)

您已在“error.response”中收到回复错误。你只需要解决这个承诺。

而不是

.catch(err => {
                console.log('request failed', err);
                dispatch(failSent("Error on sending request: " + err));
});

使用

.catch(err => {
        err.response.json().then((json) =>{
           let {Errors,Result} = json;
           dispatch(failSent(Errors)); // You are sending your array of errors here
        });
});