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

标签 javascript firebase react-native firebase-realtime-database

我尝试使用 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)

函数如下所示:

 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());
  }

这里出了什么问题?

最佳答案

当发生网络错误时,来自 fetch API 的 promise 将被拒绝,并出现 TypeError,这意味着您必须处理该错误。示例:

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}`);
});

更多信息请点击:https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful

关于javascript - 可能的未处理的 Promise 拒绝 - React-Native with Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39553274/

相关文章:

for 循环中的 JavaScript indexOf

javascript - 比较表单提交时的复选框选中状态与页面加载状态

android - 在 firebase 数据库中保存数据后如何进入下一个 Activity

java - 如何在android RecyclerView中将onBindViewHolder与多个项目一起使用

javascript - 带 React Native 导航的可触摸 onPress 不起作用

javascript - 如何在运行时了解加载的 d3.js 扩展及其版本?

javascript - 如何使用 Javascript 查找特定的隐藏 HTML 标签

swift - 使用 Swift 3.0 从随机子路径从 Firebase 检索数据

javascript - 用于添加到购物车的 redux concat 数组

react-native - 如何在运行时 react native 检测低性能设备