脱机失败且不回退到本地存储时获取 Firestore 文档



我在Expo React Native中使用FireStore。

我的代码:

import * as firebase from "firebase";
import '@firebase/firestore';
store = {
uid: null,
data: null
}
firebase.initializeApp(firebaseConfig);
export async function startUpAsync() {
await firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
try {
await new Promise((resolve => {
try {
firebase.auth().onAuthStateChanged(user => {
store.uid = user?.uid;
resolve();
})
}
catch (e) {
logError(e);
return null;
}
}));
const doc = await firebase.firestore().collection('my-collection').doc(store.uid).get();
if (doc.exists) {
store.data = doc.data();
}
else {
store.data = {...initalData};
}
return store.data;
}
catch (e) {
console.error(e);
return null;
}
}

我正在尝试在没有互联网连接的情况下测试我的代码。

我第一次使用连接运行我的应用程序,验证和获取文档都很好。然后我在setPersistence之后的startUpAsync中添加了以下行:

firebase.firestore().disableNetwork()

身份验证工作正常,但是获取文档失败。我本想从本地存储中获取文档。消息:

Failed to get document because the client is offline.

我的Firebase配置中缺少什么吗?

版本:

"@firebase/firestore": "1.12.1"
"expo": "37.0.0"
"firebase": "7.9.0"
"react": "16.9.0"
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.0.tar.gz"

我猜这也可能是我的模拟器的问题,还没有在物理设备上测试。

我错过了配置调用

firebase.firestore().enablePersistence()

最新更新