gpt4 book ai didi

android - 无法使用 android 模拟器上的 Firestore 模拟器访问云 Firestore 后端

转载 作者:行者123 更新时间:2023-12-03 23:06:03 25 4
gpt4 key购买 nike

我的问题是我无法使用 Android 模拟器上的 firebase firestore 模拟器从客户端调用 firestore。我已经测试了多个 Android 模拟器。
我收到的错误表明该应用程序根本无法连接到 Firestore,声称没有互联网连接。以下是完整消息:“@firebase/firestore:, Firestore (7.8.1): 无法到达 Cloud Firestore 后端。连接失败 1 次。最近的错误:FirebaseError: [code=unavailable]: 操作无法完成
这通常表明您的设备目前没有健康的互联网连接。客户端将在离线模式下运行,直到它能够成功连接到后端。”
这让我很困惑,因为我的应用程序可以与 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如果是,我也不确定如何在保留在世博会管理的工作流程中时修复它。
这是我的火力基地配置
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:5001') 替换 firebase.functions().useFunctionsEmulator('http://localhost:5001')
10.0.2.2 是从 Android 模拟器连接到主机的“localhost”的特殊 IP 地址。 https://firebase.google.com/docs/emulator-suite/connect_and_prototype#android_1

关于android - 无法使用 android 模拟器上的 Firestore 模拟器访问云 Firestore 后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62664699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com