我将 expo 用于 react-native 并希望在我的 React-native 应用程序中初始化 firebase auth。 有以下错误需要修复:
TypeError: (0, _reactNative.getReactNativePersistence) is not a function. (In '(0, _reactNative.getReactNativePersistence)(_asyncStorage.default)', '(0, _reactNative.getReactNativePersistence)' is undefined)
我的firebase初始化文件如下:
import { initializeApp, FirebaseApp, getApps, getApp } from 'firebase/app';
import { Auth, getAuth, initializeAuth } from 'firebase/auth';
import { getReactNativePersistence } from 'firebase/auth/react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { initializeFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";
// Your web app's Firebase configuration
const firebaseConfig = {
...app
};
let app;
let auth;
if (getApps().length < 1) {
app = initializeApp(firebaseConfig);
auth = initializeAuth(app, {
persistence: getReactNativePersistence(AsyncStorage),
});
} else {
app = getApp();
auth = getAuth();
}
const db = initializeFirestore(app, {
experimentalForceLongPolling: true,
});
const storage = getStorage(app);
export {db, auth, storage};
这是我的 package.json 文件:
{
"name": "...",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web"
},
"dependencies": {
"@react-native-firebase/app": "~15.4.0",
"@react-native-segmented-control/segmented-control": "^2.4.0",
"@react-navigation/bottom-tabs": "^6.3.3",
"@react-navigation/native-stack": "^6.8.0",
"expo": "~46.0.9",
"expo-av": "~12.0.4",
"expo-camera": "~12.3.0",
"expo-linear-gradient": "~11.4.0",
"expo-status-bar": "~1.4.0",
"expo-video-thumbnails": "~6.4.0",
"firebase": "^9.1.0",
"lottie-react-native": "5.1.3",
"react": "18.0.0",
"react-native": "0.69.6",
"react-native-animatable": "^1.3.3",
"react-native-expo-cached-image": "^1.3.1",
"react-native-keyboard-aware-scroll-view": "^0.9.5",
"styled-components": "^5.3.5",
"@react-native-async-storage/async-storage": "~1.17.3"
},
"devDependencies": {
"@babel/core": "^7.12.9"
},
"private": true
}
我使用 Firebase 9.1,因为 EXPO 文档 建议这样做。我使用 _reactNative.getReactNativePersistence 的原因是为了防止出现警告:
Firebase on React Native should import AsyncStorage from @react-native-community/async-storage
作为参考,您可以在此处找到警告问题: 问题
在 Firebase
getReactNativePersistence()
中添加了
v9.4.1
。您的 package.json
不太可读,但似乎是 9.1.0
。尝试使用 npm i firebase@latest
更新到最新版本。
我实际上遇到了这个问题,并且从完整路径导入它对我有用,尽管打字稿尚未检测到该功能。希望它对某人有用
// @ts-expect-error Some error with types in this import because of the versions
import { getReactNativePersistence } from '@firebase/auth/dist/rn/index.js';