我最近在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返回值,所以在初始化时它只注册第一个项目.

推荐答案

您的初始化代码看起来不必要地复杂.只是这两款应用都有一个唯一的名字,不用担心判断getApps():

const app = initializeApp(firebaseConfig, "admin");
const app2 = initializeApp(firebaseConfig2, "client");

Javascript相关问答推荐

Next.js(react)使用moment或不使用日期和时间格式

WebRTC关闭navigator. getUserMedia正确

将字符串UTC Date转换为ngx—counting leftTime配置值的数字

无法访问Vue 3深度监视器中对象数组的特定对象值'

如何在JavaScript文件中使用Json文件

使用POST请求时,Req.Body为空

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

如何在Svelte中从一个codec函数中调用error()?

查询参数中的JAVASCRIPT/REACT中的括号

为什么客户端没有收到来自服务器的响应消息?

如何在 Select 文本时停止Click事件?

获取';无法解决导入和导入";slick-carousel/slick/slick-theme.css";';错误

如何利用CSS中的隐藏元素实现平滑扩展和防止网格行间隙

有效路径同时显示有效路径组件和不存在的路径组件

自定义图表工具提示以仅显示Y值

使用线性插值法旋转直线以查看鼠标会导致 skip

JQuery使用选项填充HTMLSELECT并设置默认结果,默认结果显示为空

Firebase函数中的FireStore WHERE子句无法执行

在每次重新加载页面时更改指针光标

如何调整下拉内容,使其不与其他元素重叠?