0

我有一个使用火力的应用,整个堆叠好看多了,功能,数据库,存储,身份验证,消息,整个9我想保持客户端非常轻巧。因此,如果用户对帖子发表评论并“标记”了其他用户,那么使用典型的“@username”风格标记,我将所有繁重的工作都转移到了firebase功能上。这样客户端就不必根据用户名来计算用户ID,并且执行其他操作。这是设置使用触发器,所以当出现上述情况发生了,我写的名为“create_notifications”有一些数据的“表”像删除计算之后到来的写事件火力功能

{ 
    type: "comment", 
    post_id: postID, 
    from: user.getUid(), 
    comment_id: newCommentKey, 
    to: taggedUser 
} 

凡taggedUser是用户名,帖子ID是活动后,该newCommentKey从注释数据库引用的.push()中检索,user.getUid()来自firebase auth类。

现在在我的火力功能,我必须为获取所有的相关信息,并发出通知后的所有相关细节的海报特定表“onWrite”触发。所有这些都是完整的,我想弄清楚的是......如何删除传入的事件,这样我就不需要任何类型的cron作业来清除此表。我可以抓住的情况下,做我所需要的计算和数据收集,发送消息,然后删除传入的事件,因此它永远不会在除了时间的少量花了收集数据的数据库,即使真的存在。

的火力功能的简化样本触发是...

exports.createNotification = functions.database.ref("/create_notifications/{notification_id}").onWrite(event => { 
    const from = event.data.val().from; 
    const toName = event.data.val().to; 
    const notificationType = event.data.val().type; 
    const post_id = event.data.val().post_id; 
    var comment_id, commentReference; 
    if(notificationType == "comment") { 
    comment_id = event.data.val().comment_id; 
    } 

    const toUser = admin.database().ref(`users`).orderByChild("username").equalTo(toName).once('value'); 
    const fromUser = admin.database().ref(`/users/${from}`).once('value'); 
    const referencePost = admin.database().ref(`posts/${post_id}`).once('value'); 

    return Promise.all([toUser, fromUser, referencePost]).then(results => { 
    const toUserRef = results[0]; 
    const fromUserRef = results[1]; 
    const postRef = results[2]; 

    var newNotification = { 
     type: notificationType, 
     post_id: post_id, 
     from: from, 
     sent: false, 
     create_on: Date.now() 
    } 
    if(notificationType == "comment") { 
     newNotification.comment_id = comment_id; 
    } 

    return admin.database().ref(`/user_notifications/${toUserRef.key}`).push().set(newNotification).then(() => { 
     //NEED TO DELETE THE INCOMING "event" HERE TO KEEP DB CLEAN 
    }); 
    }) 
} 

所以在它的最终“回归”这个函数,它后完成数据写入到“/ user_notifications”表,我需要删除开始整个事件的事件。有谁知道这是怎么做到的吗?谢谢。

回答

0

实现这一目标的最简单方法是通过调用由Admin SDK中 你可以通过事件得到参考notification_id提供的remove()功能,即event.params.notification_id然后将其删除时,需要与admin.database().ref('pass in the path').remove();,你是好走。

3

首先,使用.onCreate代替.onWrite。您只需要在每个孩子首次写作时阅读,这样可以避免不良的副作用。有关可用触发器的更多信息,请参阅文档here

event.data.ref()持有事件发生的参考。您可以拨打remove()上,参照其删除:

return event.data.ref().remove()

+0

如果我改变它交给的onCreate我还会使用相同的路径,仍然有{} notification_id PARAM? –