创建firebase-admin的多个实例

Pau*_*Wit 7 node.js firebase firebase-realtime-database

我有多个Firebase数据库,我想创建一个连接到所有数据库的管理员.要初始化应用程序,我首先需要Firebase-admin模块并初始化应用程序.如果我使用不同的凭据再次运行它,它仍然只会初始化一个应用程序.

var admin = require("firebase-admin");
...


Object.keys(config).map(function (k, i){

var c = config[k];

var serviceAccount = require(c.credential);

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    apiKey: c.apiKey,
    authDomain: c.authDomain,
    databaseURL: c.databaseURL,
    storageBucket: c.storageBucket
}, c.name);
...
}
Run Code Online (Sandbox Code Playgroud)

这不起作用!即使有两种配置,也只会初始化一个应用程序.

jwn*_*ngr 10

// Initialize the default app
firebase.initializeApp(defaultAppConfig);

// Initialize another app with a different config
var otherApp = firebase.initializeApp(otherAppConfig, "other");

console.log(defaultApp.name);  // "[DEFAULT]"
console.log(otherApp.name);    // "other"

// Use the shorthand notation to retrieve the default app's services
var defaultAuth = firebase.auth();
var defaultDatabase = firebase.database();

// Use the otherApp variable to retrieve the other app's services
var otherAuth = otherApp.auth();
var otherDatabase = otherApp.database();
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看Firebase Admin SDK设置指南的初始化多个应用文档.


Pau*_*Wit 5

弄清楚了...

var app = admin.initializeApp({... })

var db = admin.database(app);
Run Code Online (Sandbox Code Playgroud)

  • 还应将“ app”作为第二个参数添加:var app = admin.initializeApp({...},'app'); (2认同)