when using firebase cloud function, let say if I want to insert some field into the firebase, I use ref.child().set(), but when I want to update the child, I can use both ref.child().update() and ref.child().set(). Is that a correct way to use ref.child().set() to update the field if the field is existed and to insert the field if the field is not exist? Because if I use ref.child().update(), I need to make sure the field data is inside the firebase. Thank you?
Add a comment
|
1 Answer
There is no difference in writing the initial value of a node and updating it. Both can be accomplished with either set() or update().
The difference between ref.set(...) and ref.update({...}) is that the first replaces everything that currently exists at ref, while the second updates only the keys/paths that are specified in the parameters.
I highly recommend checking out the documentation on updating nodes for an example.