首页 > 解决方案 > 将本地模拟器用于带有 expo 的 firebase

问题描述

我正在尝试使用 firebase 在本地进行模拟,以测试我正在使用 expo 开发的 react 本机应用程序。为此,我正在尝试将功能和 Firestore 的主机设置为本地主机上的正确端口。

经过多次迭代,我终于找到了一个奇怪的导入和调用组合,没有出错。但是,当我尝试使用 expo 运行它时,我的应用程序出现了一个没有错误的空白屏幕。在这一点上我很迷茫,firebase 文档令人困惑。

这是我当前的 index.js:

import firebase from "firebase"
import 'firebase/firestore';
import 'firebase/functions';
import 'firebase/app';


const config = {
    //config info
  };
const fb = firebase.initializeApp(config);
const firestore = firebase.firestore();
const functions = firebase.functions();

if(__DEV__){
  firestore.settings({
    host : "localhost:9000",
    ssl : false
  });
  functions.useFunctionsEmulator("http://localhost:5001");
}
const auth = fb.auth();

export { auth, functions, firestore }

导入非常奇怪,我不理解它们,但我是从另一个堆栈溢出线程中得到它们的,这是唯一让它甚至到达文件末尾的东西。关于设置本地模拟器,我是否缺少一些非常明显的东西?

标签: firebasereact-nativeexpo

解决方案


一起使用 Local Emulator Suite 和 Expo 时可能遇到的问题是“localhost”是指您正在使用的设备。如果您在物理设备上进行测试,则需要将 Firebase 指向您计算机上正在运行的实例。我在这里写了一个简短的解释器:

https://dev.to/haydenbleasel/using-the-firebase-local-emulator-with-expo-s-managed-workflow-5g5k


推荐阅读