我的 Firebase 项目包含多个 Firestore 数据库,我需要在代码中在这些数据库之间切换。
const response = await firestore().collection(collectionId).doc(savedId).get().then((doc: any) => {
console.log(doc.data(), 'doc.data()');
return doc.data();
});
这会连接到默认数据库,但是如何提及数据库Id?
我尝试创建另一个应用程序实例,但即使我无法找到与在 React Native 中为 Firestore 切换多个数据库 ID 相关的任何文档或文章。
在node js中,我可以在同一项目内的不同databaseId之间切换。
const { Firestore } = require('@google-cloud/firestore');
var firestore = new Firestore({
projectId: projectId,
databaseId: databaseId,
keyFilename: keyFilenamePath
});
首先,确保您的 React Native 项目中安装了 Firebase 依赖项:
npm 安装@react-native-firebase/app @react-native-firebase/firestore
在您的 Firebase 项目中,为您要使用的每个 Firestore 数据库创建不同的配置。每个配置应包括 apiKey、authDomain、projectId、storageBucket、messagingSenderId 和 appId。
使用各自的配置初始化多个 Firebase 应用实例。
import firebase from '@react-native-firebase/app';
import '@react-native-firebase/firestore';
const firebaseConfig1 = {
apiKey: 'YOUR_API_KEY_1',
authDomain: 'YOUR_AUTH_DOMAIN_1',
projectId: 'YOUR_PROJECT_ID_1',
storageBucket: 'YOUR_STORAGE_BUCKET_1',
messagingSenderId: 'YOUR_MESSAGING_SENDER_ID_1',
appId: 'YOUR_APP_ID_1',
};
const firebaseConfig2 = {
apiKey: 'YOUR_API_KEY_2',
authDomain: 'YOUR_AUTH_DOMAIN_2',
projectId: 'YOUR_PROJECT_ID_2',
storageBucket: 'YOUR_STORAGE_BUCKET_2',
messagingSenderId: 'YOUR_MESSAGING_SENDER_ID_2',
appId: 'YOUR_APP_ID_2',
};
// Initialize the default app
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig1);
}
// Initialize secondary app
const secondaryApp = firebase.initializeApp(firebaseConfig2, 'secondary');
从每个应用实例访问 Firestore 实例。
const firestoreDefault = firebase.firestore(); const firestoreSecondary = secondaryApp.firestore();
根据您需要访问的数据库使用相应的 Firestore 实例。
const useDefaultDatabase = true; // Change this flag to switch databases
const firestore = useDefaultDatabase ? firestoreDefault : firestoreSecondary;
// Example usage
firestore.collection('your-collection').get()
.then(querySnapshot => {
querySnapshot.forEach(documentSnapshot => {
console.log(documentSnapshot.data());
});
});
示例组件 下面是一个示例组件,演示了两个 Firestore 数据库之间的切换:
import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';
import firebase from '@react-native-firebase/app';
import '@react-native-firebase/firestore';
const firebaseConfig1 = {
// Your first Firebase configuration
};
const firebaseConfig2 = {
// Your second Firebase configuration
};
// Initialize the default app
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig1);
}
// Initialize secondary app
const secondaryApp = firebase.initializeApp(firebaseConfig2, 'secondary');
// Firestore instances
const firestoreDefault = firebase.firestore();
const firestoreSecondary = secondaryApp.firestore();
const FirestoreSwitcher = () => {
const [useDefaultDatabase, setUseDefaultDatabase] = useState(true);
const firestore = useDefaultDatabase ? firestoreDefault : firestoreSecondary;
const fetchData = async () => {
const snapshot = await firestore.collection('your-collection').get();
snapshot.forEach(doc => {
console.log(doc.data());
});
};
return (
<View>
<Button title="Switch Database" onPress={() => setUseDefaultDatabase(!useDefaultDatabase)} />
<Button title="Fetch Data" onPress={fetchData} />
</View>
);
};
export default FirestoreSwitcher;