Reputation: 2538
I'm not sure I'm doing this correctly.
I'd like to remove the parent of a node if the variable parameter within the node is true..
This is what I have:
exports.removePost = functions.database
.ref('posts/{locationId}/{postId}/score')
.onUpdate(async snapshot => {
const score = snapshot.after.val();
if(score < -4)
return false;
if(score >= -4)
return snapshot.ref.parent().remove();
return false;
});
So score
is an integer and if it is equal to or greater than -4 onUpdate
I'd like to remove the parent, so {postId}
.
I'm guessing this is possible. But when I try to deploy the code above it says that ref does not exist on type 'Change<DataSnapshot>'.
Why does it say that?
Also, as a side question -- if I set the firebase security rules so that only a user with the same uid
as the postId
's uid
, will the above code still work?
Upvotes: 0
Views: 308
Reputation: 317467
It looks like you're expecting the first argument to your onUpdate handler function to be DataSnapshot object, but it is not. The first argument to onUpdate and onWrite handler function is a Change object with before
and after
fields that describe the contents of the database at the trigger location before and the change that triggered it. These fields are DataSnapshot objects with a ref
field.
If you want the ref of the location that changed, try this instead:
exports.removePost = functions.database
.ref('posts/{locationId}/{postId}/score')
.onUpdate(async change => {
const score = change.after.val();
if (score < -4) {
return null;
}
else {
return change.after.ref.parent.remove();
}
});
Upvotes: 2