typescript - 在 Firebase 中使用 onUpdate 函数时,如何检索返回的快照的子节点?
问题描述
我有过去使用 .onCreate 运行的波纹管代码,我将其更改为 .onUpdate,现在 .child() 行出现错误,表明我无法执行该操作。我该如何进行?我需要访问这些值。
export const sendNewMessageNotification = functions.database
.ref('/ChatTimelines/{uid}/{chatID}/')
.onUpdate((snapshot, context) => {
//.onCreate
const uid = context.params.uid
const otherUID = snapshot.child("otherUID").val()//error
const message = snapshot.child("lastMessage").val()//error
snapshot
//addNewLikeNotif(uid, newLikerUID, mediaNumber, timeStamp, postID)
return sendNewMessageNotificationFunc(uid, otherUID, message)
})
更新:Update2 这不起作用
(变化,上下文)
let snapshot = change.after.val()
const otherUID = snapshot.child("otherUID").val()
const message = snapshot.child("lastMessage").val()
解决方案
如果您使用onUpdate
,则需要执行以下操作:
.onUpdate((snapshot, context) => {
const uid = context.params.uid
const otherUID = snapshot.after.child("otherUID").val();
const message = snapshot.after.child("lastMessage").val();
检查文档:
https://firebase.google.com/docs/functions/beta-v1-diff#sdk_changes_by_trigger_type
推荐阅读
- python - “str”对象没有属性“find_all”
- python - 操作数不能与shapes(4,)(3,)一起广播,在python2.7中
- stata - coefplot:将回归的名称放在 y 轴上
- linux - 使用 bash/shell 在 echo 中显示 2 个变量的疯狂问题
- android - 无法在 Google Play 上上传 App Bundle
- python - 合并字典(相同键),其中包含 python 3.7 中的列表
- java - 在 Spring Boot 中通过 RestTemplate POST JSON 对象
- ios - 在 ARKit 中获取点云点的颜色
- javascript - Javascript 创建帐户未链接到 Firebase 数据库
- python - 将 ssh 隧道命令翻译成 SSHTunnelForwarder 代码?