After adding a post about a person to a Firebase database, I want to add the reference to the new post to the person. However the person may or may not already exist.
I have:
var ref = new Firebase("https://mydatabase.firebaseio.com/");
var _person = document.getElementById("Person").value;
var _remark = document.getElementById("Remark").value;
var postsRef = ref.child("remarks");
var newPostRef = postsRef.push({
person: _person,
remark: _remark
});
var postID = newPostRef.key();
var personRef = ref.child("person");
personRef.update({
_person: postID
});
However that creates a node called _person
in child person instead of the value of the _person
variable. Using set()
would overwrite an existing person.
Example:
First a node remarks/-JlkbxAKpQs50W7r84gf
is created with child node person/123456
After that I want to create a node person/123456
(only if it doesn't already exist) and than add a child node remark/-JlkbxAKpQs50W7r84gf
to it. The post-id is automatically generated (Firebase) but the person's id is to be taken from a html form.
How do I do that?