简体   繁体   English

提取API响应未定义

[英]Fetch api response being undefined

This question has been asked but none of the answers helped. 有人问了这个问题,但没有一个答案有帮助。

In react native I'm making an api call with this: 在React Native中,我正在使用此API进行调用:

  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: 在邮递员中,我可以确认这项工作: 在此处输入图片说明

However console.log(result) keeps returning as undefined. 但是console.log(result)继续返回未定义状态。 Any ideas what I'm doing wrong? 有什么想法我做错了吗?

Your syntax is a bit off and your then block is not part of your fetch request. 您的语法有点过时, then块就不是fetch请求的一部分。 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);
      });
  });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM