简体   繁体   English

可能未处理的 Promise 拒绝 - React-Native with Firebase

[英]Possible Unhandled Promise Rejection - React-Native with Firebase

I am trying to make a fetch API call to a new Firebase instance with my React-Native app, but I am running into this error:我正在尝试使用 React-Native 应用程序对新的 Firebase 实例进行 fetch API 调用,但我遇到了以下错误:

Possible Unhandled Promise Rejection (id: 0):
Network request failed
TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:25119:8)
    at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:10405:15)
    at XMLHttpRequest.setReadyState (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26688:6)
    at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26536:6)
    at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:26630:52
    at RCTDeviceEventEmitter.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:9638:23)
    at MessageQueue.__callFunction (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7493:34)
    at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7375:8
    at guard (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7306:1)
    at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:7374:1)

The functions look like so:函数看起来像这样:

 getNotes(username){
    username = username.toLowerCase().trim();
    var url = `https://myproject-6342.firebaseio.com/${username}.json`;
    return fetch(url).then((res) => res.json());
  },
  addNote(username, note){
    username = username.toLowerCase().trim();
    var url = `https://myproject-6342.firebaseio.com/${username}.json`;
    return fetch(url, {
      method: 'post',
      body: JSON.stringify(note)
    }).then((res) => res.json());
  }

What is going wrong here?这里出了什么问题?

The promise from the fetch API will reject with a TypeError when a network error occurs, which means you have to handle the error.当发生网络错误时,来自 fetch API 的承诺会以TypeError拒绝,这意味着您必须处理错误。 Example:例子:

function _handleError(errorMessage) {
  console.log(errorMessage);
}

fetch('https://api.github.com/users/octocat/repos').then(function(response) {
  if(response.ok) {
    return response.json();
  } else {
    _handleError(`Oops, something went wrong: ${response.status}, ${response.statusText}`);
  }
}).then(function(data) {
   if(data) {
     console.log('success', data);
   }
}).catch(function(error) {
  _handleError(`There has been a problem with your fetch operation: ${error.message}`);
});

More info here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful更多信息: https : //developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful

Well in my case the emulator internet was not working, I followed the below thread to make that work.好吧,在我的情况下,模拟器互联网无法正常工作,我按照以下线程进行操作。 Android emulator not able to access the internet Android模拟器无法访问互联网

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

相关问题 反应本地可能未处理的诺言拒绝 - Possible unhandled promise rejection in react-native (本机)可能未处理的承诺拒绝FBSDK - (React-Native) Possible Unhandled Promise Rejection FBSDK 可能未处理的 Promise 拒绝 (id:0) | 反应原生 firebase 谷歌登录 - Possible Unhandled Promise Rejection (id:0) | React native firebase google login 反应本机可能未处理的Promise拒绝 - React Native possible unhandled Promise rejection 反应本机可能未处理的承诺拒绝(id:0): - React Native Possible Unhandled Promise Rejection (id: 0): React Native:可能未处理的Promise拒绝(id:0) - React Native: Possible Unhandled Promise Rejection (id: 0) 可能未处理的 promise Rejection (id:0) React Native - Possible unhandled promise Rejection (id:0) React Native Axios 承诺处理 - 在 react-native 中获取“可能未处理的承诺拒绝 - 类型错误:网络请求失败” - Axios Promise Handling - Getting “Possible Unhandled Promise Rejection - TypeError: Network request failed” in react-native React Native Unhandled promise 拒绝 | React-Native, 异步, - React Native Unhandled promise rejection | React-Native, Async, React Native中未处理的承诺拒绝 - Unhandled promise rejection in react native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM