简体   繁体   中英

How to copy a childNode from one node to another?

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.

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});
   }

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

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

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? eg survey completed, exam completed These events should be sent to firebase queue that can run a series of pipelines to update whatever you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM