这是我的 Firebase 数据库结构:
project
|
|- Users
|
| - 00001
|
| - Likes
| |
| | - 00001
| |
| | - 'whatever':'whatever'
|
| - Unlikes
|
| - 00002
|
| - 'whatever':'whatever'
我想要的是一个数据库触发函数,当用户添加到 Likes
id 为 00002
的项目时,它会自动从 Unlikes
中删除.
我正在使用 JavaScript,这是我现在拥有的功能:
exports.likeItem = functions.database.ref('/Users/{userId}/Likes/{itemId}'.toString()).onCreate((like, context) => {
return like.ref.parent.child('/Unlikes').once('value', (unlikes) => {
unlikes.ref.child(context.params.itemId).set(null);
});
});
该函数在写入 like 时正确触发,但不会删除 unlike。
错误是这样的:
'parent' of undefined at exports.likeItem.functions.database.ref.onCreate
最佳答案
如果我正确理解了您要实现的目标,那么以下内容应该可以解决问题。
exports.likeItem = functions.database
.ref('/Users/{userId}/Likes/{itemId}')
.onCreate((like, context) => {
return like.ref.parent.parent
.child('/Unlikes/' + context.params.itemId)
.set(null);
});
请注意,您不需要使用 once()
方法读取任何内容。只需定义要删除的正确引用(使用 like.ref.parent.parent.child(....)
)并将其设置为 null
。
关于javascript - 如何删除具有云功能的 Node ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55679795/