React Native:未处理的promise promise:TypeError:undefined不是对象(评估' response.json')

时间:2018-01-06 14:53:13

标签: javascript react-native

当我试图在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.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获取文本,或者我做错了什么?谢谢。

1 个答案:

答案 0 :(得分:3)

呃,你为什么要用两个.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));
        });
    }
})