javascript - 未处理的 promise 拒绝: Error: Network Error

标签 javascript api react-native axios backend

我正在尝试从 API 获取数据。

我这样做了:

const testScreen=()=>{

const c =  axios.get("http://localhost:3000/unverifiedProperty/warehouse",{
    headers:{
        'Authorization':'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InN1dGhhcmhpbWFzbmh1OThAZ21haWwuY29tIiwiaWF0IjoxNTgzODY0NjgwfQ.fxclNhIaNkTnINwOinqFRitX_AA7nQCrLtaFBLY99Tc'
    }
})
console.log(c)

return(
    <View>
        <Text>This is test Screen</Text>

    </View>
)}

我想使用 get 请求获取数据。

在终端中,我得到这个:

  Running application on Himanshu.
  Promise {
  "_40": 0,
  "_55": null,
  "_65": 0,
  "_72": null,
  }

[Unhandled promise rejection: Error: Network Error]
- node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\axios\lib\adapters\xhr.js:78:22 in handleError
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:574:29 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue

我已经使用 postman 确保我没有遇到网络问题。您可以查看此屏幕截图: enter image description here

请在这里帮助我。

最佳答案

首先,功能测试屏幕右括号(})丢失

其次 axios.get(或任何其他方法)返回一个 Promise 。 要访问你的 c 你需要做类似的事情。 如果 promise 失败,它将被缓存在 .catch 方法中,您可以记录它并查看是否存在问题

const c =  axios.get("http://localhost:3000/unverifiedProperty/warehouse",{
    headers:{
        'Authorization':'Bearer 0000000'
    }
}).then(res=>res)
.catch(err=>err)

这里是 Promise 如何工作的链接 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

关于javascript - 未处理的 promise 拒绝: Error: Network Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60631143/

相关文章:

javascript - values.join 不是文件 http.es5.js Angular 4 中的函数

php - 在我的网页上用PHP显示API结果

scroll - 文本输入占位符在 native react 中下降

ios - 守望者:Library/LaunchAgents/com.github.facebook.watchman.plist 用于写入:不是目录

React-native-calendars 使用 renderArrow 函数显示上个月和下个月而不是箭头

javascript - 为什么这个 jasmine-node 测试没有失败?

javascript - 是否可以在 body 负载上触发鼠标悬停

javascript - 需要替换对象值

api - 谷歌音译的限制是多少?

rest - 免费 Rest API 以字符串形式检索当前日期时间(与时区无关)