简体   繁体   English

在同一NodeJS应用程序中使用firebase.database()和admin.database()

[英]Using firebase.database() and admin.database() in same NodeJS application

Most of my application is using firebase.database() and I have only used admin where elevated permissions were required. 我的大多数应用程序都使用firebase.database(),并且仅在需要提升权限的地方使用admin。 I just updated to Firebase 4.12.1, once I did that, I started receiving the following error when I used admin.database(): 我刚刚更新到Firebase 4.12.1,一旦这样做,当我使用admin.database()时开始收到以下错误:

[2018-04-16T13:45:01.042Z] @firebase/database: FIREBASE FATAL ERROR: Database initialized multiple times. [2018-04-16T13:45:01.042Z] @ firebase / database:FIREBASE FATAL ERROR:数据库已初始化多次。 Please make sure the format of the database URL matches with each database() call. 请确保数据库URL的格式与每个database()调用匹配。

I found that if I remove calls for firebase.database() that happen prior to admin.database() I no longer get the issue, but that connection if I go to a subsequent page that does user firebase.database(), I receive the error because the connection already exists. 我发现,如果删除在admin.database()之前发生的对firebase.database()的调用,我将不再遇到问题,但是如果我转到执行用户firebase.database()的后续页面,则该连接会收到该错误,因为该连接已经存在。

Is this a bug, am I doing something incorrectly, or can we not use firebase.database() and admin.database() both in the same node application after Firebase 4.10.0(last working version I have)? 这是错误吗?我做错了什么吗?还是在Firebase 4.10.0(我拥有的上一个工作版本)之后,我们不能在同一节点应用程序中同时使用firebase.database()和admin.database()吗?

app.js(truncated) app.js(被截断)

var config = {
  apiKey: "XXXX",
  authDomain: "XXXX",
  databaseURL: "https://XXXX.firebaseio.com",
  projectId: "XXXX",
  storageBucket: "XXXX",
  messagingSenderId: "XXXX"
};
firebase.initializeApp(config);

serviceAccount = require(path.join(__dirname, 'XXXXX.json'));
var database = firebase.database();

users.js users.js

var express = require('express');
var router = express.Router();
var firebase = require("firebase");

var admin = require("firebase-admin");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://XXXX.firebaseio.com"
});
/* GET users listing. */
router.get('/', function(req, res, next) {
  var databaseRef = admin.database().ref('navigator/users');
  var query = databaseRef.once('value').then(function(snapshot) {
    var totalUsers = 0;
    snapshot.forEach(function(element) {
      if(element.child('locations').hasChild('branson')){
        console.log(element.key);
        console.log('');
        totalUsers += 1;
      }
    });
    res.send('totalUsers' + totalUsers);
  });
});

module.exports = router;

You should not use the Admin SDK and the regular Firebase JavaScript SDK in a single page. 应该使用管理SDK和常规火力地堡的JavaScript SDK在单一页面。 In fact, the Admin SDK is meant for use in Node.js projects, not in web pages. 实际上,Admin SDK是供在Node.js项目中使用的,而不是在网页中使用的。

While it is possible to use the Admin SDK in a web page in some cases, I'd seriously consider against it. 尽管在某些情况下可以在网页中使用Admin SDK,但我还是会认真考虑一下。 Unless you know exactly what is being accessed where, you run a risk of someone getting hold of your administrative credentials and gaining full access to your project. 除非您确切知道在哪里可以访问什么,否则就有冒着他人获取管理凭据并获得对项目的完全访问权的风险。 At the very least you'll want to host the pages that use the Admin SDK in a place that cannot be accessed by a regular user. 至少,您需要将使用Admin SDK的页面托管在普通用户无法访问的地方。

If you continue using the Admin SDK in a web page, you should then use a page reload when transitioning from a page that uses the client SDK to a page that uses the Admin SDK. 如果继续在网页中使用Admin SDK,则在从使用客户端SDK的页面过渡到使用Admin SDK的页面时,应该使用页面重新加载。 That way each page will have only one or the other loaded, and the conflict won't arise. 这样,每个页面将只加载一个页面或另一个页面,并且不会发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM