如何在 Cloud Functions for Firebase 中访问多个实时数据库实例 [英] How to access multiple Realtime Database instances in Cloud Functions for Firebase

本文介绍了如何在 Cloud Functions for Firebase 中访问多个实时数据库实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Firebase 项目中使用多个数据库.主(默认)数据库的云函数工作得很好,但是,我不能让它们为辅助数据库工作.例如,我想在具有管理员权限的节点上发出读取请求:

I'm using multiple databases in a Firebase project. Cloud functions for the main (default) database work great, however, I cannot make them work for a secondary database. For example I want to make a read request on a node with admin privileges:

//this works
admin.database().ref(nodePath).once('value')...

这在主数据库中有效,但是,如果我想在另一个数据库上执行该命令,则不起作用:

This works in the main database, however, if I want to execute the command on another database, it doesn't work:

//this doesn't work
admin.database(secondaryDatabaseUrl).ref(nodePath).once('value')...

虽然已经部署了函数,但是我在尝试执行云函数时在控制台上出现错误.

Although the functions are deployed, I get an error on the console when trying to execute the cloud function.

这是带有 https 触发器的云函数的代码:

Here's the code for the cloud function with an https trigger:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const secureCompare = require('secure-compare');

exports.testFunction= functions.https.onRequest((req, res) => {
  const key = req.query.key;
  // Exit if the keys don't match
  if (!secureCompare(key, functions.config().cron.key)) {
    console.error('keys do not match');
    res.status(403).send('error1');
    return;
  }
  //test read request
  //the line below crashes the function
  return admin.database('https://secondary_db_url.firebaseio.com').ref(`/testNode`).once('value').then(dataSnapshot=> {
    console.log('value', dataSnapshot.val());
    return;
  }).catch(er => {
    console.error('error', er);
    res.status(403).send('error2');
  });
});

以下是 Firebase 控制台中的错误日志:

Below is the error log in the Firebase console:

TypeError: ns.ensureApp(...).database is not a function
    at FirebaseNamespace.fn (/user_code/node_modules/firebase-admin/lib/firebase-namespace.js:251:42)
    at exports.testFunction.functions.https.onRequest (/user_code/index.js:16:16)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:41)
    at /var/tmp/worker/worker.js:671:7
    at /var/tmp/worker/worker.js:655:9
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

如果我不指定辅助数据库 URL,该函数将在我的主数据库上发出读取请求,效果很好:

If I don't specify the secondary database URL, the function will make the read request on my main database which works great:

//this works
return admin.database().ref(`/testNode`).once('value').then(dataSnapshot=> {
...

我使用的是最新的 SDK 版本:"firebase-admin": "^5.5.1""firebase-functions": "^0.7.3"

I'm using the latest SDK versions: "firebase-admin": "^5.5.1" and "firebase-functions": "^0.7.3"

那么,如何使用管理员权限在云函数中获取辅助数据库的实例?

So, how do I get an instance of a secondary database in cloud functions using admin privileges?

推荐答案

以下是使用 Admin SDK 通过 URL 访问数据库的方法:

Here's how to access database by URL using Admin SDK:

let app = admin.app();
let ref = app.database('https://secondary_db_url.firebaseio.com').ref();

以下是 Admin SDK 集成测试的示例:https://github.com/firebase/firebase-admin-node/blob/master/test/integration/database.js#L52

Here's an example from Admin SDK integration tests: https://github.com/firebase/firebase-admin-node/blob/master/test/integration/database.js#L52

这篇关于如何在 Cloud Functions for Firebase 中访问多个实时数据库实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆