Reputation: 1912
This is my Firebase Database structure:
project
|
|- Users
|
| - 00001
|
| - Likes
| |
| | - 00001
| |
| | - 'whatever':'whatever'
|
| - Unlikes
|
| - 00002
|
| - 'whatever':'whatever'
What I want is a DB triggered function that when the user adds to Likes
the item with id 00002
, removes it automatically from Unlikes
.
I am using JavaScript and this is the function I've got right now:
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);
});
});
The function is triggered correctly when the like is written, but doesn't remove the unlike.
The error is this:
'parent' of undefined at exports.likeItem.functions.database.ref.onCreate
Upvotes: 1
Views: 108
Reputation: 83093
If I understood correctly what you try to achieve, the following should do the trick.
exports.likeItem = functions.database
.ref('/Users/{userId}/Likes/{itemId}')
.onCreate((like, context) => {
return like.ref.parent.parent
.child('/Unlikes/' + context.params.itemId)
.set(null);
});
Note that you don't need to read anything with the once()
method. Just define the correct reference to delete (with like.ref.parent.parent.child(....)
) and set it to null
.
Upvotes: 1