Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 607
Coder1000 Avatar asked Dec 30 '16 19:12

Coder1000


1 Answers

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});
   }
like image 125
Coder1000 Avatar answered Nov 14 '22 07:11

Coder1000