[英]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.