admin管理员组

文章数量:1296452

When I am trying to parse the response body in a json so I can save it in a state, I receive the error from the title. For some reason, the response.json is undefined. Here is my response:

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: }

Here is the code where I receive the error:

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));
        })
    ]);
}

The error is received at the line where (response) => response.json() is.

Here is the fetch request:

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;
        });
};

I also tried response.text() instead of json(), but still it's undefined. How can I get the text from _bodyInit, or what am I doing wrong? Thank you.

When I am trying to parse the response body in a json so I can save it in a state, I receive the error from the title. For some reason, the response.json is undefined. Here is my response:

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: }

Here is the code where I receive the error:

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));
        })
    ]);
}

The error is received at the line where (response) => response.json() is.

Here is the fetch request:

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;
        });
};

I also tried response.text() instead of json(), but still it's undefined. How can I get the text from _bodyInit, or what am I doing wrong? Thank you.

Share Improve this question asked Jan 6, 2018 at 14:53 ArtyomskaArtyomska 1,3326 gold badges28 silver badges57 bronze badges 2
  • 2 Your first handler loginService(data).then(response => {... doesn't return anything, so the second one gets undefined. – georg Commented Jan 6, 2018 at 14:56
  • 1 You need to learn how promises work. Then you will not make the coding error you have here. – Aluan Haddad Commented Jan 6, 2018 at 14:56
Add a ment  | 

1 Answer 1

Reset to default 4

Uh, why are you using two .then calls? The former callback does not return anything, so the chained promise resolves with undefined and that's exactly what the latter callback receives. Just use only one - and also don't call response.json() when the status was not ok.

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));
        });
    }
})

本文标签: