如何在谷歌云功能中监听多个数据库 [英] How to listen to multiple databases in google cloud functions

查看:77
本文介绍了如何在谷歌云功能中监听多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的google cloud函数中收听两个数据库,所以我尝试了

I want to listen to two databases in my google cloud functions so I tried this

 const app1 = admin.initializeApp({
   credential: admin.credential.cert(serviceAccount),
   databaseURL: "https://honey.firebaseio.com"
});

const app2 = admin.initializeApp({
   credential: admin.credential.cert(serviceAccount),
   databaseURL: "https://honey-d8.firebaseio.com/"
}, 'app2'); 




// Get the default database instance for an app1
var OperationDB = admin.database().ref();

// Get a database instance for app2
var userDB = admin.database(app2).ref();

然后我尝试调用第二个数据库

then I tried to call the second database

 exports.onBoardNewUser = functions.userDB.ref('/Users/').onCreate((snapshot, context) => {

 })

每当我尝试部署文件时,都会出现此错误

whenever I attempt to deploy the file it gives this error

无法读取未定义的属性'ref'

Cannot read property 'ref' of undefined

两个数据库在同一个项目中

The two databases are in the same project

我尝试了各种变化,但没有运气.我在做什么错,该如何解决?

I have tried different variations but no luck. what am I doing wrong an how can I fix this ?

推荐答案

您不能将Cloud Function附加到某些任意数据库引用中. Cloud Functions数据库触发器与您通过客户端SDK获得的侦听器不同.它从实时数据库接收事件的发生.

You can't attach a Cloud Function to some arbitrary database reference. Cloud Functions database triggers are not like listeners that you get with the client SDKs. It receives events from Realtime Database as they are generated.

您必须使用提供的API 来构建引用了函数的函数特定的数据库和接收更新的路径.

You have to use the provided API to build a function that references a particular database and path to receive updates.

您不能在部署该功能的项目之外引用数据库.

You can't reference a database outside of the project where you deployed the function.

您可以在同一项目中引用数据库的多个分片.为此,您必须使用提供的API指定分片实例的名称.如果不使用instance()方法在函数构建器中命名该实例,则仅会接收来自主/默认分片的事件.

You can reference multiple shards of a database within the same project. To do that, you have to specify the name of the shard instance using the provided API. If you don't name the instance in the function builder using the instance() method, you will only receive events from the primary/default shard.

如果要在每个分片中的相同位置处理所有分片的事件,则必须为每个分片导出一个函数,并且每个分片都可以委派给同一助手函数来处理事件.

If you want to handle events for all of your shards at the same location in each one, you have to export one function for each shard, and they can each delegate to the same helper function to handle the event.

这篇关于如何在谷歌云功能中监听多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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