gpt4 book ai didi

javascript - react native : Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.json' )

转载 作者:行者123 更新时间:2023-11-30 11:25:41 24 4
gpt4 key购买 nike

当我尝试解析 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));
});
}
})

关于javascript - react native : Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'response.json' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48128543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com