[英]Cloud Functions for Firebase: how to reference a key in a list that has been just pushed
我的问题是,如果您要引用客户端调用push()时生成的密钥,则基本上是在云函数中做什么。
/providerApps/{UID}/
是我到达约会节点列表的路径,因此每个约会节点位于/providerApps/{UID}/someKey
。
我需要“列表中的新项目”,它是与push()一起添加的,所以我认为我可以对键进行排序并简单地获取最后一个,但这是行不通的:
// (Try to) Listen for new appointments at /providerApps/{pUID}
// and store the appointment at at /clientApps/{cUID}
// cUID is in the new appointment node
exports.storeNewAppForClient = functions.database.ref("/providerApps/{UID}").onWrite(event => {
// Exit when the data is deleted.
if (!event.data.exists()) {
console.log("deletion -> exiting");
return;
}
const pUID = event.params.UID;
const params = event.params;
console.log("params: ", params);
const firstAppVal = event.data.ref.orderByKey().limitToLast(1).val();
// TypeError: event.data.ref.orderByKey(...).limitToLast(...).val is not a function
const date = firstAppVal["dateStr"];
const cUID = firstAppVal["clientUID"];
return event.data.ref.root.child("clientApps").child(cUID).child(date).set(pUID);
});
我想我可以使用push()。getKey()在客户端执行此操作,并允许提供程序写入clientApps节点,但这似乎不太优雅。
有什么想法如何使用云功能做到这一点?
作为说明,我的数据结构如下所示:
干杯
将触发位置更改为新创建的约会,而不是约会列表。 然后,您可以直接访问约会数据:
exports.storeNewAppForClient = functions.database.ref("/providerApps/{UID}/{pushId}").onWrite(event => {
// Exit when the data is deleted.
if (!event.data.exists()) {
console.log("deletion -> exiting");
return;
}
const pUID = event.params.UID;
const params = event.params;
console.log("params: ", params);
const date = event.data.child('dateStr').val();
const cUID = event.data.child('clientUID').val();
return admin.database().ref('clientApps').child(cUID).child(date).set(pUID);
});
(针对弗兰克的评论进行了更新)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.