React native expo, 您试图通过调用 firebase.app() 来使用未安装在 Android 项目中的 Firebase 模块



在我的项目中,我使用React原生firebase来获取Analytics,我的项目是expo裸流,我只是遵循了这个https://rnfirebase.io/文档,但在运行应用程序时出现错误,如

backend.js:2173 Possible Unhandled Promise Rejection (id: 3):
Error: You attempted to use a firebase module that's not installed on your Android project by calling firebase.app().
Ensure you have:
1) imported the 'io.invertase.firebase.app.ReactNativeFirebaseAppPackage' module in your 'MainApplication.java' file.
2) Added the 'new ReactNativeFirebaseAppPackage()' line inside of the RN 'getPackages()' method list.
See http://invertase.link/android for full setup instructions.
Error: You attempted to use a firebase module that's not installed on your Android project by calling firebase.app().
Ensure you have:
1) imported the 'io.invertase.firebase.app.ReactNativeFirebaseAppPackage' module in your 'MainApplication.java' file.
2) Added the 'new ReactNativeFirebaseAppPackage()' line inside of the RN 'getPackages()' method list.

我的Android主应用程序.java

import io.invertase.firebase.app.ReactNativeFirebaseAppPackage; <---added for firebase   
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfignfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
List<ReactPackage> packages = new PackageList(this).getPackages();
packages.add(new ModuleRegistryAdapter(mModuleRegistryProvider));
packages.add(new ReactNativeFirebaseAppPackage());<---- added for firebase
return packages;
}

软件包.json

"@react-native-firebase/analytics": "^13.0.1",
"@react-native-firebase/app": "^13.0.1",
"@types/npm": "^7.19.0",
"apisauce": "^0.15.1",
"base-64": "^0.1.0",
"expo": "~42.0.1",
"expo-blur": "~9.0.3",
"expo-camera": "^11.2.2",
"expo-linear-gradient": "~9.2.0",
"expo-secure-store": "~10.2.0",
"expo-splash-screen": "~0.11.2",
"expo-status-bar": "~1.0.4",
"expo-updates": "~0.8.1",
"native-base": "^2.15.2",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "~0.63.4",

我尝试了配置插件https://docs.expo.dev/guides/config-plugins/也没有用,错误没有解决。

问题

我刚刚遇到这个问题并设法解决了它。基本上,如果你使用ExpoGo,你就不能依赖react原生firebase包(例如:@react-native-firebase/functions(。

以前,我试图在我的应用程序中使用云功能,如下所示:

import functions from '@react-native-firebase/functions';
// some other code
functions()
.httpsCallable('annotateImage')({
image: {
content: result.assets[0].base64,
},
features: [
{
type: 'TEXT_DETECTION',
},
],
imageContext: {
languageHints: ['en'],
},
}).then(// blah blah...

我得到了与OP完全相同的错误。但仔细观察后,我在阅读Expo Docs 时看到了这一点

React Native Firebase需要自定义的本地代码,不能与Expo Go一起使用。

终于找到了解决问题的线索。

修复

由于我们不能在ExpoGo项目中使用react原生firebase库,因此我们必须使用普通的firebase JS SDK。

我在我的项目中安装了sdk,就像上面的sdk链接中提到的那样。我已经有了一个firebase配置文件,但如果没有,你也需要遵循这一步骤。然后我简单地把上面的代码改成了这个

import {
getFunctions,
httpsCallable,
HttpsCallableResult,
} from 'firebase/functions';
// some other code
const functions = getFunctions();
const annotateImage = httpsCallable(functions, 'annotateImage');
annotateImage({
image: {
content: result.assets[0].base64,
},
features: [
{
type: 'TEXT_DETECTION',
},
],
imageContext: {
languageHints: ['en'],
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}).then(// blah blah...

一切都开始正常工作了!

我有一个expo应用程序,并使用了这个链接。。。

如果您退出expo或开始使用bareworkflow ,可能会有所不同

https://docs.expo.dev/guides/using-firebase/#using-带消防仓库的博览会

有一个firebase.js文件,并导入需要的不同模块

以我们的为例

import { initializeApp } from "firebase/app"
import { getFirestore } from 'firebase/firestore';
import Constants from "expo-constants"
import "firebase/auth"
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration -- from firebase console
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: xxxxxxx,
authDomain: xxxxxxx,
databaseURL: xxxxxxxxx,
projectId: xxxxxxxxx,
storageBucket: xxxxxxxxx,
messagingSenderId: xxxxxxxxx,
appId: xxxxxxxxx,
measurementId: xxxxxxxxx,
}
// Initialize Firebase
const Firebase = initializeApp(firebaseConfig)
const firestore = getFirestore(); <--- our added firestore module
export default [Firebase, firestore]

请确保您已根据FB软件包安装指南添加了@firebase/模块名称

相关内容

  • 没有找到相关文章

最新更新