在Firebase中结合使用onDisconnect和云功能 [英] Combine onDisconnect with Cloud Functions in Firebase
问题描述
假设用户可以写入房间",并且当用户断开连接时应该删除这些值,我认为云功能代码应如下所示:
Let's say a user can write to "rooms" and the values should remove when the user disconnects, I thought the cloud function code would look like this:
exports.Rooms = functions.database.ref('/rooms/{values}').onWrite(event => {
if (!event.data.exists()){return}
return db.ref('rooms/' + `${event.params.values}`).onDisconnect().remove()
}
})
但是,当用户在将值写入房间"后断开连接时,什么也没有发生.当用户断开连接时,如何删除写入的路径?
But when the user disconnects after he wrote values to 'rooms', nothing happens. How can I remove the path written to when the user disconnects?
谢谢.
推荐答案
onDisconnect()
在Cloud Functions中无法使用.在客户端这很有意义,因为客户端是断开连接的事情.如果要让功能在客户端断开连接时触发,则必须在客户端上设置onDisconnect()
,将内容写入触发该功能以对该事件做出反应的位置.
onDisconnect()
doesn't make sense to use from Cloud Functions. It makes sense on the client side, since the client is the thing doing the disconnecting. If you want a function to trigger when the client disconnects, you'll have to set up the onDisconnect()
on the client side, the have that write to a location that triggers a function to react to that event.
这篇关于在Firebase中结合使用onDisconnect和云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!