无法使用android emulator上的firestore emulator访问云firestore后端

无法使用android emulator上的firestore emulator访问云firestore后端,android,firebase,react-native,google-cloud-firestore,expo,Android,Firebase,React Native,Google Cloud Firestore,Expo,我的问题是,我无法使用Android模拟器上的firebase firestore emulator从客户端调用firestore。我已经测试了多个Android模拟器 我收到的错误表明该应用程序根本无法连接到firestore,声称没有互联网连接。以下是完整消息:“@firebase/firestore:,firestore(7.8.1):无法访问云firestore后端。连接失败1次。最近的错误:FirebaseError:[代码=不可用]:操作无法完成 这通常表示您的设备目前没有正常的In

我的问题是,我无法使用Android模拟器上的firebase firestore emulator从客户端调用firestore。我已经测试了多个Android模拟器

我收到的错误表明该应用程序根本无法连接到firestore,声称没有互联网连接。以下是完整消息:“@firebase/firestore:,firestore(7.8.1):无法访问云firestore后端。连接失败1次。最近的错误:FirebaseError:[代码=不可用]:操作无法完成 这通常表示您的设备目前没有正常的Internet连接。客户端将在脱机模式下运行,直到能够成功连接到后端。”

这让我很困惑,因为我的应用程序与iOS上的firebase firestore emulator配合使用。它还可以与android的firebase云函数模拟器(Nevermind,它从未使用过云函数)一起使用。如果有必要的话,当在生产中使用firestore时,该应用程序在iOS和Android上也能正常工作

我正在使用Expo管理的工作流创建React本机应用程序,因此我正在使用firebase的web SDK。这意味着我必须弹出才能切换到反应本机firebase

我也看了这些文件,我似乎在遵循其中的说明:

我也遇到过这个问题,我不清楚它们是否相关。如果他们是,我也不知道如何在保持世博会管理工作流的同时修复它

这是我的firebase配置

firebaseConfig.js

import * as firebase from 'firebase';
// Optionally import the services that you want to use
//import "firebase/auth";
//import "firebase/database";
import "firebase/firestore"; // uncommented so I could test locally w/ emulator
import "firebase/functions";
//import "firebase/storage";

// ios id 
  // appId: "1:586841249272:ios:d8b508f7811d7c84e0b20d",

// Initialize Firebase
const firebaseConfig = {
  apiKey: "myApikey",
  authDomain: "fsp2-a670d.firebaseapp.com",
  databaseURL: "https://fsp2-a670d.firebaseio.com",
  projectId: "fsp2-a670d",
  storageBucket: "fsp2-a670d.appspot.com",
  messagingSenderId: "586841249272",
  appId: "1:586841249272:android:fa68525699ea5cdde0b20d"
};

// added .firestore to test firestore locally w/ emulator 
const db = firebase.initializeApp(firebaseConfig).firestore(); 

// for debugging
firebase.firestore.setLogLevel('debug')

// Uncomment the below line to use cloud functions with the emulator
firebase.functions().useFunctionsEmulator('http://localhost:5001')
// firebase.firestore().settings({ experimentalForceLongPolling: true });

// uncomment this to test firestore locally w/ emulator 
  db.settings({
    host: "localhost:8080",
    ssl: false
  });
这是客户端调用firestore失败的文件中的代码。是的

const More = ({navigation}) => {
  const [userInfo, setUserInfo] = useState({profilePicture: 'placeholder', userName: ''});

  useEffect(() => {
    const changeUserInfo = async () => {
      const userData = await new Promise(function (resolve, reject) {
        // 2 - Copy-paste your code inside this function
        firebase.auth().onAuthStateChanged(user => {
          resolve(user) // this promise is rejected and I need to write the code to handle rejections
        })
      })
      const db = firebase.firestore();
      const uid = userData?.uid;
      if (uid) {
        const userRef = await db.collection('users').doc(uid);
        const user = await userRef.get();
        const userFields = user.data();
        console.log('userFields is: ', userFields);
        const {profilePicture, userName} = userFields;
        console.log('profilePicture is: ', profilePicture);
        console.log('userName is: ', userName);
        setUserInfo(() => {
          return {
            profilePicture,
            userName,
          }
        });
      }
    }
    changeUserInfo()
  }, [userInfo.profilePicture, userInfo.userName]

我真的很感激任何帮助

在db.settings中,我需要将主机设置为“10.0.2.2:8080”

为了让云函数正常工作,我需要替换firebase.functions().useFunctionsEmulator('http://localhost:5001“”与firebase.functions()一起使用。useFunctionsEmulator('http://10.0.2.2:5001")


10.0.2.2是从Android仿真器连接到主机“本地主机”的特殊IP地址

我通过使用Expo的内置调试器主机变量解决了这个问题,该变量也适用于物理iOS设备:
常量.manifest.debuggerHost?.split(“:”).shift()

在这里写了一个简短的解释:


嘿!您不应该在此处发布API凭据@JuanCarlosDurini你确定吗?看了这篇文章我觉得没关系:谢谢!事后看来,localhost显然无法在模拟器/设备上工作。在我的例子中,当使用真正的电话时,我需要使用计算机的IP而不是10.0.2.2。