最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase update or set - Stack Overflow

programmeradmin9浏览0评论

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("/");
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?

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?

Share Improve this question edited Sep 5, 2022 at 16:15 zabdalimov 387 bronze badges asked Mar 31, 2015 at 16:46 user1837293user1837293 1,6664 gold badges20 silver badges35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 14

Depending on how your data is structured, before you update, you may be able to get a reference to the person you want.

So, if your data looks something like this:

{
    "remarks" : {
                   ...
                },

    "person" : {
                 "123456" : {
                                "name" : "foo",
                                ...
                                "blah" : "bar"
                            },
                 ...
               }
}

And document.getElementById("Person").value gives you 123456, you can get the reference like:

var personRef = ref.child("person").child(_person);

Then you want to see if it exists, and if it does, update it:

personRef.once('value', function(snapshot) {

    if( snapshot.val() === null ) {
        /* does not exist */
    } else {
        snapshot.ref.update({"postID": postID});
    }

});
发布评论

评论列表(0)

  1. 暂无评论