0

I try extract data and status request from request in react native, when utilize this code

function postUser(FirstName, LastName, Phone, Email, Password) {
    let data = {
        method: 'POST',
        credentials: 'same-origin',
        mode: 'same-origin',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            email: Email,
            password: Password,
            first_name: FirstName,
            last_name: LastName,
            phone: Phone
        })
    }
    return fetch(URLPostUser, data)
        .then(response => response.json())
} 

in this result don't had status.

when change the return of function, i try get status, but in this case don't have access to data

function postUser(FirstName, LastName, Phone, Email, Password) {
    let data = {
        method: 'POST',
        credentials: 'same-origin',
        mode: 'same-origin',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            email: Email,
            password: Password,
            first_name: FirstName,
            last_name: LastName,
            phone: Phone
        })
    }
    return fetch(URLPostUser, data)
}

2 Answers 2

1

You can do the following

return fetch(URLPostUser, data)
    .then(response => ({ response, json: response.json()}))

Now the promised object is

{
    response: // the response object
    json: // the parsed JSON
}

Or if you don't want the whole response

return fetch(URLPostUser, data)
    .then(response => ({ status: response.status, json: response.json()}))

The resulting promise is of the object

{
    status: // the response object
    json: // the parsed JSON
}
Sign up to request clarification or add additional context in comments.

Comments

0

https://stackoverflow.com/a/39339648/8533250

Those link was helpful for me. Because previously answer return

{ status: 200, json: Promise})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.