How to listen to multiple databases in google cloud functions

我是研究僧i 提交于 2019-12-25 01:12:34

问题


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

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 ?


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/56671834/how-to-run-the-same-firebase-functions-on-two-different-databases-in-the-same-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!