1

This question has been asked but none of the answers helped.

In react native I'm making an api call with this:

  getAuthToken = () => {

  SecureStore.getItemAsync('authToken')
  .then((authToken) => {
    console.log(authToken);
    fetch('https://example.com?token=' + authToken + '&order_id=5480', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      }
    }
)
})
.then(res => res.json())
.then(result => {
    console.log(result)
  })
.catch(error => {
    console.error(error);
})
}

In post man I can confirm this works: enter image description here

However console.log(result) keeps returning as undefined. Any ideas what I'm doing wrong?

1
  • Postman has 8 headers set while you only have 2 set. This could cause the issue. Commented Jul 2, 2019 at 21:28

1 Answer 1

2

Your syntax is a bit off and your then block is not part of your fetch request. Update to the following and it should work

getAuthToken = () => {
  SecureStore.getItemAsync("authToken").then(authToken => {
    console.log(authToken);
    fetch("https://example.com?token=" + authToken + "&order_id=5480", {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      }
    })
      .then(res => res.json())
      .then(result => {
        console.log(result);
      })
      .catch(error => {
        console.error(error);
      });
  });
};
Sign up to request clarification or add additional context in comments.

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.