我最近在Stack Overflow上问了一个关于这个问题的问题,但这个问题因为重复的原因而关闭.我try 了他们建议的每一种解决方案,甚至在Firebase文档中也是如此.但它没有起作用,所以这就是为什么我再次问这个问题.
我的问题是,我想合并我以前的Firebase项目,以便在一个管理应用程序中进行管理.在Firebase文档中,它建议在初始化Firebase配置时,我们必须为其中一个配置指定唯一的名称,我这样做了,但它没有读取它.那么,我如何将我的两个独立项目合并到一个应用程序中呢?
在我的firebase.js
里
import { initializeApp, getApp, getApps } from "firebase/app";
import { getFirestore } from "firebase/firestore/lite";
import { getStorage } from "firebase/storage";
const firebaseConfig = {
apiKey: "AIzaSy...",
authDomain: "admin-side.firebaseapp.com",
projectId: "admin-side",
storageBucket: "admin-side.appspot.com",
messagingSenderId: "5434...",
appId: "1:543...:web:aa001...",
};
const firebaseConfig2 = {
apiKey: "AIzaSyB...",
authDomain: "client-side.firebaseapp.com",
projectId: "client-side",
storageBucket: "client-side.appspot.com",
messagingSenderId: "74150...",
appId: "1:741...:web:7614...",
};
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);
const app2 = getApps().length
? getApp()
: initializeApp(firebaseConfig2, "secondary");
const db = getFirestore(app);
const storage = getStorage(app);
const db2 = getFirestore(app2);
const storage2 = getStorage(app2);
export { db, storage, db2, storage2 };
这就是我拿它的方式
import { db, db2 } from "../firebase";
import {
collection,
getCount,
getDocs,
limit,
query,
startAfter,
where,
} from "firebase/firestore/lite";
export default function HomePage(){
const getData = async () => {
const constraints = [];
constraints.push(limit(10));
// in db there is a collection called "blogPosts"
// and in db2 there is a collection called "posts"
// when i call "posts" with db2 it returns empty array.
// when i call "blogPosts" with db it returns the value
// and when i call "blogPosts" with db2 it returns the value from
// db, so in initialization it only registers the first project
const posts = collection(db2, "posts");
let q = query(blogs, ...constraints);
const qSnapshot = await getDocs(q);
const dataQ = qSnapshot.docs.map((doc) => ({
...doc.data(),
id: doc.id,
}));
console.log(dataQ); // this returns an empty array
setDatas(dataQ);
setLoader(false);
}
}
在db中有一个名为"blogpost"的集合,在DB2中有一个名为"post"的集合.当我用DB2调用"POSTS"时,它返回空array.当我用db调用"blogpost"时,它返回值,而当我用DB2调用"blogpost"时,它从db返回值,所以在初始化时它只注册第一个项目.