How to copy a childNode from one node to another?

寵の児 提交于 2020-01-03 08:39:30

问题


SITUATION:

I would need to download the childNode, then set() it into the other node.

Problem is I want to do this only once the childNode's score attribute reaches 100.

Where and when should I check if posts have a score of 100 or more and how would I copy them to the new index only once ?


WHAT I THOUGHT OF:

When a post is loaded, check for it's score. If it's >= 100, check in the database if that's the case. Then push the node to the new index.


PROBLEM:

How would I prevent the node from being uploaded each time the post is loaded since it's score is >= 100 on multiple loads ? I need it to happen only once !


SOLUTION CODE:

if (funPost.score >= global.hotNumber && funPost.hot == false) {
            var hotPostRef = firebase.database().ref("hot/section/"+key);
            var hotPost = {
                title: funPost.title,
                image: funPost.image,
                id: funPost.id,
                key: funPost.key
            }
            hotPostRef.set(hotPost);
            funPostRef.update({"hot": true});
        }
   else if (funPost.score <= (global.hotNumber - 25) && funPost.hot == true) {
        var hotPostRef = firebase.database().ref("hot/section/"+key);
        hotPostRef.remove();
        funPostRef.update({"hot": false});
   }

Solution: I ended up using a boolean flag.


回答1:


I ended up doing it with a boolean flag:

if (funPost.score >= global.hotNumber && funPost.hot == false) {
            var hotPostRef = firebase.database().ref("hot/section/"+key);
            var hotPost = {
                title: funPost.title,
                image: funPost.image,
                id: funPost.id,
                key: funPost.key
            }
            hotPostRef.set(hotPost);
            funPostRef.update({"hot": true});
        }
   else if (funPost.score <= (global.hotNumber - 25) && funPost.hot == true) {
        var hotPostRef = firebase.database().ref("hot/section/"+key);
        hotPostRef.remove();
        funPostRef.update({"hot": false});
   }



回答2:


Try using .once() insted of .on().

    ref.once('value')
       .then(function(dataSnapshot) {
       // handle read data.
     });



回答3:


https://gist.github.com/katowulf/6099042 Is how you copy or move ref to another ref.

How would I prevent the node from being uploaded each time the post is loaded since it's score is >= 100 on multiple loads ? I need it to happen only once !

1) Use bolt rules to fail the write if the path key already exists() in the new location.

2) What causes the score to go up? e.g. survey completed, exam completed These events should be sent to firebase queue that can run a series of pipelines to update whatever you need.



来源:https://stackoverflow.com/questions/41401812/how-to-copy-a-childnode-from-one-node-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!