创建firebase-admin

时间:2016-11-16 06:31:53

标签: 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);
...
}

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

2 个答案:

答案 0 :(得分:8)

// 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();

有关详细信息,请查看Firebase Admin SDK设置指南的Initialize mutltiple apps文档。

答案 1 :(得分:4)

想出来......

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

var db = admin.database(app);