7

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.

Coder1000
  • 4,071
  • 9
  • 35
  • 84
  • 2
    You appear to be building the `>= 100` index when you are *reading* the data. Is there a reason for this? Have you considered building the index when writing the data? Is there a reason you cannot? Doing so is relatively straight forward. – cartant Jan 03 '17 at 07:09
  • If you add an observer to the node that contains the score, it will notify your app when it's updated. So when it notifies your app, check if it's 100 and if so, download the node, the score to 0 and perform the copy. If it's a one shot deal you could simply have your app check a preferences node that contains a node /should_observe: true and only observe the scores node if that is true, if false, don't add an observer. However, downloading the entire node and writing it back seems like overkill. Why are you doing that? – Jay Jan 03 '17 at 13:33
  • @Jay If you look at my res.render(), you will see I am passing the entire post to display info on the next page. But all is fine: I ended up using a boolean flag. Will post my code tonight. – Coder1000 Jan 03 '17 at 14:39
  • @cartant I thought about it. But the moment I write data is generated client-side. I prefer to hide the algorithm to get posts in the top section in my server code. Hence why I check on load :) – Coder1000 Jan 03 '17 at 17:32

3 Answers3

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});
   }
Coder1000
  • 4,071
  • 9
  • 35
  • 84
0

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

    ref.once('value')
       .then(function(dataSnapshot) {
       // handle read data.
     });
Razvan Alex
  • 1,706
  • 2
  • 18
  • 21
  • That's precisely what I had already thought of. But it will still be triggered every time the page is loaded. – Coder1000 Dec 30 '16 at 20:48
  • On the server-side, when the page is loaded, so is the score of the post. If the score is >= 100, then set post to new location. Normally I would use a boolean flag to make sure this only happens once, but I don't see where I would put it here :/ – Coder1000 Dec 30 '16 at 20:53
  • If the score loses its value on refresh you have to save it in a database and display it from there, especailly on server side. – Razvan Alex Dec 30 '16 at 21:00
  • How does this have anything to do with my question ? Besides, this is already how my website is coded. – Coder1000 Dec 30 '16 at 21:04
  • There should be a workaround, but until I see some source code it will be hard to figure it out. I've got something in my mind with socket.io – Razvan Alex Dec 30 '16 at 21:12
  • Is it Single Page Application? As I've got user works with it for some time. And once when post's score goes above 100, the operation should be performed. Could you track current post's score? And when `funPostOnThePage.score < 100 and funPost > 100` trigger an action? BTW: what should be done if score goes below and back again? – Eugene Lisitsky Jan 01 '17 at 19:56
  • @EugeneLisitsky I ended up using a boolean flag. Found a way to make it efficient. Will post code tonight. – Coder1000 Jan 03 '17 at 11:15
-1

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.

Leblanc Meneses
  • 3,001
  • 1
  • 23
  • 26