[英][Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.data')]
[英]React Native: Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.json')
当我试图在json中解析响应体,所以我可以将它保存在一个状态,我收到标题中的错误。 出于某种原因,response.json未定义。 以下是我的回复:
16:28:30: Response {
16:28:30: "_bodyInit": "\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImFkbWluIiwibmJmIjoxNTE1MjQ4OTEwLCJleHAiOjE1MTUyNTAxMTAsImlhdCI6MTUxNTI0ODkxMH0.8zIX0PjC0XHBiHTtXlDvNxbOADj2NgtxK8zC-MqxoCg\"",
16:28:30: "_bodyText": "\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImFkbWluIiwibmJmIjoxNTE1MjQ4OTEwLCJleHAiOjE1MTUyNTAxMTAsImlhdCI6MTUxNTI0ODkxMH0.8zIX0PjC0XHBiHTtXlDvNxbOADj2NgtxK8zC-MqxoCg\"",
16:28:30: "headers": Headers {
16:28:30: "map": Object {
16:28:30: "access-control-allow-credentials": Array [
16:28:30: "true",
16:28:30: ],
16:28:30: "cache-control": Array [
16:28:30: "public, max-age=0",
16:28:30: ],
16:28:30: "content-length": Array [
16:28:30: "182",
16:28:30: ],
16:28:30: "content-type": Array [
16:28:30: "application/json; charset=utf-8",
16:28:30: ],
16:28:30: "date": Array [
16:28:30: "Sat, 06 Jan 2018 14:28:30 GMT",
16:28:30: ],
16:28:30: "expires": Array [
16:28:30: "-1",
16:28:30: ],
16:28:30: "server": Array [
16:28:30: "Microsoft-IIS/10.0",
16:28:30: ],
16:28:30: "x-aspnet-version": Array [
16:28:30: "4.0.30319",
16:28:30: ],
16:28:30: "x-powered-by": Array [
16:28:30: "ASP.NET",
16:28:30: ],
16:28:30: },
16:28:30: },
16:28:30: "ok": true,
16:28:30: "status": 200,
16:28:30: "statusText": undefined,
16:28:30: "type": "default",
16:28:30: "url": "url",
16:28:30: }
这是我收到错误的代码:
export function loginAction(data) {
return dispatch => Promise.all([
dispatch(loginStarted()),
loginService(data).then(response => {
console.log(response);
if (!response.ok) {
Alert.alert('ERROR', 'User or password is incorrect');
dispatch(loginFailed('User or password is incorrect'));
}
else {
Alert.alert('OK', 'Login is a success');
}
}).then((response) => response.json()).then((response) => {
console.log(response);
dispatch(loginSuccess(response));
})
]);
}
在(response)=> response.json()所在的行收到错误。
这是获取请求:
export const loginService = (user) => {
return fetch(`${httpApiUrl}/api/userdata/ReactVerify`, {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/json',
},
body: JSON.stringify(user)
})
.then(function (response) {
return response;
});
};
我也尝试了response.text()而不是json(),但它还是未定义的。 如何从_bodyInit获取文本,或者我做错了什么? 谢谢。
呃,你为什么要用两个.then
电话? 前一个回调函数不会返回任何内容,因此链接的promise将以undefined
而这正是后一个回调所接收的。 只使用一个 - 当状态不正常时也不要调用response.json()
。
loginService(data).then(response => {
console.log(response);
if (!response.ok) {
Alert.alert('ERROR', 'User or password is incorrect');
dispatch(loginFailed('User or password is incorrect'));
} else {
Alert.alert('OK', 'Login is a success');
return response.json().then(data => {
console.log(data);
dispatch(loginSuccess(data));
});
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.