Gwa*_*r17 5 android firebase react-native expo google-cloud-firestore
我的问题是我无法使用 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如果是这样,我也不确定如何在保持世博会管理的工作流程的同时修复它。
这是我的 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
});
Run Code Online (Sandbox Code Playgroud)
这是文件中从客户端调用 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]
Run Code Online (Sandbox Code Playgroud)
我真的很感激任何帮助!
在 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
| 归档时间: |
|
| 查看次数: |
2560 次 |
| 最近记录: |