简体   繁体   English

Firebase的云功能:如何为所有子项的特定子键触发数据库功能

[英]Cloud Functions for Firebase: How do I trigger a database function for a specific child key of all children

Let's say my database looks like the following: 假设我的数据库如下所示:

project
└── users
    ├── safuhf8sdf76fs
    │   ├── name: 'William'
    │   └── favoriteColor: 'blue'
    ├── sffuef5srf72fd
    │   ├── name: 'Emily'
    │   └── favoriteColor: 'yellow'
    └── rfdu4ffsgf42gi
        ├── name: 'Sam'
        └── favoriteColor: 'red'

I'm trying to create a Cloud Function that is triggered when the favoriteColor value of any user object is changed/added/removed. 我正在尝试创建一个在更改/添加/删除任何user对象的favoriteColor值时触发的云功能。

The part I'm having trouble with is creating the reference to said key. 我遇到问题的部分是创建对所述密钥的引用。

I know that I could listen to any and all changes to users with 我知道我可以听取用户的任何和所有更改

exports.updatedFavoriteColor = functions.database.ref('/users').onWrite(event => { ... });

and then just check the data changed to see if it was favoriteColor . 然后只需检查更改的数据,看它是否是favoriteColor

While this would work, I'd much prefer a trigger that is specifically listening for the favoriteColor key of all user objects- something kind of like this: functions.database.ref('/users').child(?).child('favoriteColor') 虽然这可行,但我更喜欢一个专门监听所有用户对象的favoriteColor键的触发器 - 类似这样的东西: functions.database.ref('/users').child(?).child('favoriteColor')

My preferable function would be called when the favoriteColor of William or Emily is changed, but not when the name of Sam is changed, for example. 当威廉或艾米丽的favoriteColor被改变时,我会调用我更favoriteColor的函数,但是当Sam的name改变时,不会被调用。 Is this possible? 这可能吗?

I think I figured it out with some more digging. 我想我想通过更多的挖掘来解决这个问题。

The Firebase docs suggest I can do the following: Firebase文档建议我可以执行以下操作:

exports.updatedFavoriteColor = functions.database.ref('/users/{pushId}/favoriteColor').onWrite(event => { ... });

This should get triggered by the change in favoriteColor for any user, where the particular user being updated is represented by the {pushId} in the reference. 这应该由对任何用户的favoriteColor的更改触发,其中更新的特定用户由引用中的{pushId}表示。 In the function itself, you can reference the user id with event.params.pushId 在函数本身中,您可以使用event.params.pushId引用用户标识

暂无
暂无

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

相关问题 Firebase云功能数据库触发'onCreate不是函数' - Firebase Cloud Functions Database Trigger 'onCreate is not a function' 如何使用云功能查找特定子级的Firebase实时数据库 - How to look for a specific child the Firebase real time database using cloud functions 如何使用Firebase云功能从Firebase数据库检索子节点的键值? - How to retrieve key value of child node from firebase database using firebase cloud function? 如何在云端功能中查询Firebase数据库? - How do I query a firebase database inside a cloud function? Firebase Cloud Database触发器功能并非总是能完成 - Firebase Cloud Database Trigger Functions not always completing 如何使用 Javascript 云从 Firebase 实时数据库中随机获取子密钥名称 function - How to get a child key name randomly from the Firebase Realtime database with a Javascript cloud function 如何在使用 Cloud Functions 时使用 Firebase Admin SDK 更改 Firebase 实时数据库中的数据? - How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions? 如何根据子 Cloud 函数删除 Firebase 中的某个父节点 - How do I remove a certain parent node in Firebase based on a child Cloud functions 如何在将子项添加到数据库时触发云功能? - How to trigger Cloud Function only when child is added to the database? 如何从 Firebase 实时数据库中获取数据,这些数据是随机密钥的子项 - 使用本机反应? - How do I fetch data from firebase realtime database which are children to a random key - using react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM