Javascript 如何维护web应用程序的多个firebase DBs并从中获取数据
我在web上有一个web应用程序(带有react.js),最近在BE系统上实现了firebase切分 现在我的任务是从多个DB URL读取数据(目前可以是3个不同的) 因此,在与一位高级开发人员讨论后,我们实现了Javascript 如何维护web应用程序的多个firebase DBs并从中获取数据,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我在web上有一个web应用程序(带有react.js),最近在BE系统上实现了firebase切分 现在我的任务是从多个DB URL读取数据(目前可以是3个不同的) 因此,在与一位高级开发人员讨论后,我们实现了 // FirebaseClient.js file constructor(appName = 'liveApp') { const findApp = firebase.apps.find((app) => app.name === appName);
// FirebaseClient.js file
constructor(appName = 'liveApp') {
const findApp = firebase.apps.find((app) => app.name === appName);
this.app = findApp || firebase.initializeApp(firebaseConfig, appName);
this.user = null;
this._dataRefs = {};
this._typeBaseData = {};
this._emitter = new EventEmitter();
this.databaseConnectionPool = {};
this.database = null;
this.databaseEndpoints = {};
}
setDatabaseConnection(endpoint) {
if (!this.databaseConnectionPool[endpoint]) {
this.databaseConnectionPool[endpoint] = this.app.database(endpoint);
}
return this.databaseConnectionPool[endpoint];
}
getDatabaseConnection(eventId) {
let endpoint = this.databaseEndpoints[eventId];
if (!endpoint) {
endpoint = firebaseConfig.databaseURL;
}
if (!this.databaseConnectionPool[endpoint]) {
this.setDatabaseConnection(endpoint);
}
this.database = this.databaseConnectionPool[endpoint];
return this.databaseConnectionPool[endpoint];
}
setIdMapping(eventId, endpoint) {
if (!this.DatabaseEndpoints[eventId])
this.DatabaseEndpoints[eventId] = endpoint;
}
ref(eventId, type) {
return this.getDatabaseConnection(eventId).ref(`${type}`);
// return this.database.ref(`${type}`); // firebase.database(this.app).ref(`${type}`);
}
它在开发环境中运行良好,我们甚至还进行了部署和测试
但当我们部署到生产环境中时,我得到了这个错误(在图中)
它基本上说有多个数据库正在初始化
那我怎么控制它呢
起初,我尝试在代码上维护多个firebase客户端,但后来,firebase客户端在我想要呈现内容时/之前无法准备好,这导致了问题
我也提到了这一点:
FirebaseApp
实例。一旦知道客户端需要连接到的数据库碎片的URL,您需要初始化(新的)FirebaseApp
。我应该提到整个流程吗?firebaseclient.js文件显示了整个客户端,基于类和自定义方法。有一个钩子useFireabaseClient.js,它基本上通过new FirebaseClient()初始化客户端代码>然后验证并返回客户端。我在应用程序级上下文中调用这个钩子,允许我在表的每一行中使用它。这些行应该从各自的Firebase DB中获取数据。我理解您所说的,我尝试了初始化多个客户端的方法,但当组件呈现时,客户端还没有准备好。使整个应用程序崩溃@Frank我所知道的是错误告诉我的:您似乎多次使用相同的appName
和firebaseConfig
变量中不同的数据库URL调用'firebase.initializeApp(firebaseConfig,appName)`函数。这里缺少很多内容,因此很难提供帮助。一般情况下:您不能像代码尝试做的那样重新初始化相同的FirebaseApp
实例。一旦知道客户端需要连接到的数据库碎片的URL,您需要初始化(新的)FirebaseApp
。我应该提到整个流程吗?firebaseclient.js文件显示了整个客户端,基于类和自定义方法。有一个钩子useFireabaseClient.js,它基本上通过new FirebaseClient()初始化客户端代码>然后验证并返回客户端。我在应用程序级上下文中调用这个钩子,允许我在表的每一行中使用它。这些行应该从各自的Firebase DB中获取数据。我理解您所说的,我尝试了初始化多个客户端的方法,但当组件呈现时,客户端还没有准备好。使整个应用程序崩溃@Frank我所知道的只是错误告诉我的:在firebaseConfig
变量中,您似乎多次使用相同的appName
和不同的数据库URL调用'firebase.initializeApp(firebaseConfig,appName)'。