【问题标题】:How can I use firebase emulators with my react-native app?如何在我的 react-native 应用程序中使用 firebase 模拟器?
【发布时间】:2021-09-16 10:31:21
【问题描述】:

我使用以下文章设置了我的 react-native 应用并连接到 firebase:https://firebase.googleblog.com/2016/01/the-beginners-guide-to-react-native-and_84.html

我想通过在模拟器中运行 firebase 服务来测试我的更改。谁能指导我怎么做?

--编辑--

我按照https://firebase.google.com/docs/emulator-suite/connect_and_prototype 中列出的步骤,在 config.js 文件中添加了以下 sn-p:

var db = firebase.firestore();

if (location.hostname === "localhost") {
  db.useEmulator("localhost", 8080);
}

执行应用程序后,我得到以下错误:

ReferenceError: Can't find variable: location
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:171:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/expo-error-recovery/build/ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
at [native code]:null in flushedQueue
at [native code]:null in invokeCallbackAndReturnFlushedQueue

Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:171:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/expo-error-recovery/build/ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
at [native code]:null in flushedQueue
at [native code]:null in invokeCallbackAndReturnFlushedQueue

【问题讨论】:

标签: firebase react-native firebase-authentication


【解决方案1】:

在 React Native 中使用 location 是行不通的,因为没有全局的 window 变量可以从中读取。您需要其他方式来确定是要访问模拟器还是云托管项目。

如果您没有更好的条件,您可以在代码中手动切换它:

if (true) {
  db.useEmulator("localhost", 8080);
}

【讨论】:

  • 我删除了条件块,它运行正常
  • 我尝试在 firestore 中硬编码一个条目:const entityRef = firebase.firestore().collection('entities') entityRef.add({ text: 'Hello world', }) 但它抛出了一个错误。
  • 错误:@firebase/firestore:, Firestore (8.7.0): Connection, WebChannel transport errored:, me
猜你喜欢
  • 2021-02-13
  • 2020-07-23
  • 2023-02-23
  • 2021-11-10
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 2019-08-07
相关资源
最近更新 更多