【问题标题】:Create multiple instances of firebase-admin创建多个 firebase-admin 实例
【发布时间】:2017-03-30 06:12:56
【问题描述】:

我有多个 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);
...
}

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

【问题讨论】:

    标签: node.js firebase firebase-realtime-database


    【解决方案1】:

    想通了……

    var app = admin.initializeApp({... })
    
    var db = admin.database(app);
    

    【讨论】:

    • 'app' 也应该作为第二个参数添加: var app = admin.initializeApp({... }, 'app');
    【解决方案2】:
    // 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 文档了解更多详情。

    【讨论】:

    • 虽然它不是公认的答案,但这对我有用
    • 毫无疑问,这应该是公认的答案,因为它显示了如何在初始化后使用方法
    猜你喜欢
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2018-07-11
    • 2017-11-15
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多