我在firebase真实的数据库中有一个用户表,它包含以下字段:
userKey
- uid
- emailVerfied
- attempts
我想观察userKey/attempts
字段的变化,我有以下触发设置:
export const onUpdate = functions.database
.ref('/users/{uid}/attempts').onUpdate( event => {
const record = event.after.val();
// sending an email to myself,
adminLog(
`user ${record.email} requested a new confirmation email`
, `delivered from realtime.onUpdate with user ${record.uid}`);
})
这个函数在每次attempt
字段更新时都会被触发,但是它显然不能像我想的那样检索record.uid
,因为发送的电子邮件看起来像这样:
delivered from realtime.onUpdate with user undefined
检索数据库值的快照的正确方法是什么?
1条答案
按热度按时间b5lpy0ml1#
使用当前的代码和数据结构,
'/users/{uid}/attempts'
中uid
的值实际上是userKey
,而不是userKey
的子节点uid
的值。要在云函数代码中获取此值,您应该执行以下操作
因为您使用的Firebase SDK for Cloud Functions版本〈1.0.0(请参阅下文)
相反,如果你想得到
uid
的值,而不是userKey
的值,你应该在上层监听,如下所示:然后我建议您将代码升级到v1.0.0(参见文档here),然后执行以下操作: