繁体   English   中英

无法在 android 仿真器上使用 Firestore 仿真器访问云 Firestore 后端

[英]could not reach cloud firestore backend with firestore emulator on android emulator

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

我收到的错误表明该应用程序根本无法连接到 Firestore,声称没有互联网连接。 以下是完整消息:“@firebase/firestore:, Firestore (7.8.1): Could not reach Cloud Firestore backend. Connection failed 1 times. 最近的错误:FirebaseError: [code=unavailable]: The operation could not be completed This通常表明您的设备目前没有健康的互联网连接。客户端将在离线模式下运行,直到它能够成功连接到后端。

这让我很困惑,因为我的应用程序可以与 iOS 上的 firebase firestore 模拟器一起使用。 它还可以与 android 的 firebase 云功能模拟器(没关系。它从未与云功能一起使用)一起使用。 如果它很重要,当在生产中使用 firestore 时,该应用程序也可以在 iOS 和 Android 上正常工作。

我正在使用 Expo 托管工作流创建一个 React Native 应用程序,所以我使用的是 firebase 的 web SDK。 这意味着我必须弹出才能切换到 react-native-firebase。

我还查看了文档,我似乎遵循了那里的说明: https://firebase.google.com/docs/emulator-suite/connect_firestore#web

我也遇到了这个问题,我不清楚它们是否相关。 https://github.com/firebase/firebase-js-sdk/issues/2923如果是,我也不确定如何在继续使用 expo 管理的工作流程时修复它。

这是我的 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://10.0.2.2.2) 替换 firebase.functions().useFunctionsEmulator('http://localhost:5001'):

10.0.2.2 是特殊的 IP 地址,用于从 Android 仿真器连接到主机的“localhost”。 https://firebase.google.com/docs/emulator-suite/connect_and_prototype#android_1

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

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

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

虽然我自己的案例与任何 JS 库/框架无关,而是在使用 vanilla JavaScript 时,我在本地机器上使用 firebase 模拟器遇到了同样的问题。

在网站上没有任何希望的谷歌搜索后,我发现通过设置防火墙 url 并在初始化 firebase 后禁用 SSL 解决了我自己的问题。 下面的例子:

             /** After: const firestore = firebase.initializeApp("<firebase-config>").firestore() **/

                firestore.settings({
                    host: "localhost:8080",
                    ssl: false
                });

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM