firebase - React Native - 使用 @react-native-firebase/storage 将图像上传到 Firebase - 没有创建 Firebase 应用“[DEFAULT]”
问题描述
将图像上传到 Firebase 时出现以下错误:
错误:没有创建 Firebase 应用“[DEFAULT]” - 调用 firebase.initializeApp()
这是我的代码:
应用程序.js
import * as Firebase from 'firebase'; componentDidMount() { Firebase.initializeApp(firebaseConfig); }
Profile.js
import * as Firebase from 'firebase'; import rnFb from '@react-native-firebase/storage'; uploadImage = localUri => new Promise((resolve, reject) => { const localUri2 = Platform.OS === 'ios' ? localUri.replace('file://', '') : localUri; const fbUri = Firebase.storage().ref(); rnFb().ref(localUri2).putFile(fbUri) .then( () => { resolve(); } ) .catch( (e) => { reject(e); } ); });
它在生产线上失败了.putFile
。
我不明白问题出在哪里,因为我正在.initializeApp()
打电话App.js
更新 12/21
我console.log(Firebase.apps.length);
之前添加rnFb().ref(localUri2).putFile(fbUri)
了,输出为 1 ......确实很奇怪。
...如果我完全按照错误的要求进行操作并在收到错误错误firebase.initializeApp()
之前立即调用:rnFb().ref(localUri2).putFile(fbUri)
Firebase: Firebase App named '[DEFAULT]' already exists
帮助!!
解决方案
My understanding is that the Firebase SDK used internally inside @react-native-firebase
is independent of the ordinary Firebase SDK from firebase
.
It can be exposed using:
import firebase from '@react-native-firebase/app';
// OR
import { firebase } from '@react-native-firebase/storage';
Applying these changes (and simplifying your code), leaves you with the following:
import storage, { firebase } from '@react-native-firebase/storage';
// can possibly be somewhere else
firebase.initializeApp(firebaseConfig);
uploadImage = localUri => {
const localUri2 = Platform.OS === 'ios' ? localUri.replace('file://', '') : localUri;
return storage().ref('/path/to/upload/to').putFile(localUri2)
}
Rather than use "client initialization" using firebase.initializeApp()
, you can also use "native initialization"
for Android and iOS.
推荐阅读
- r - 可以使用 dygraphs 按日/月而不是完整日期对象进行绘图吗?
- flutter - 颤振initState()与build()?
- javascript - 从 div id 加载 Google Maps 标记
- java - 如何从 WebElements 列表中获取元素的数量(大小)
- sql - 使用 A Union B 与 Where A OR B 的 Postgres 性能
- android - Android - 不使用 Intent 更新联系人
- mysql - MySQL 子选择然后组不工作
- javascript - Angular 在组件内嵌套哑组件并仅显示给特定页面或父组件
- java - 升级到 Java 8 会导致单元测试中的 Orika 映射器 java.io.IOException: invalid constant type: 15 at 142
- javascript - safari input type='number' 无法显示真实值