1

I am a beginner Firebase and am having trouble breaking the loop. The code below looks at the recipe products and the quantity of products, if the inventory has enough products to make the recipe, the code triggers an alert stating that there are enough products, otherwise it triggers the alert saying that the products in stock are not enough. I would like to break the loop from the else, how can I do?

checkFirebase.orderByKey().limitToLast(20).on('child_added', function(snapshot){

    var productRecipe = snapshot.key;
    var amountRecipe = snapshot.child("amount").val() * amountAdded;

    if(productRecipe == productStock){

        if(amountRecipe <= amountStock){
            alert("The amount is enough.");
        }else{
            alert("The quantity is not enough.");
            break;
        }

    }

});
2
  • 1
    There is no loop in this code, so the break won't do anything. What do you expect it to break from? Commented Jan 10, 2017 at 4:08
  • I would like it when the code stream goes into the else, the flow is diverted to another place. I'm having a lot of trouble with asynchronous communication in Firebase. I still do not get it right. @FrankvanPuffelen Commented Jan 11, 2017 at 18:01

1 Answer 1

3

If you want to stop listening the child_added event, you can try Off.

Try this:

checkFirebase.orderByKey().limitToLast(20).on('child_added', function(snapshot){

    var productRecipe = snapshot.key;
    var amountRecipe = snapshot.child("amount").val() * amountAdded;

    if(productRecipe == productStock){

        if(amountRecipe <= amountStock){
            alert("The amount is enough.");
        }else{
            alert("The quantity is not enough.");
            checkFirebase.off('child_added');//stop listening to child_added event
        }

    }

});

For more information on off Please Read firebase References#off Documentation

Sign up to request clarification or add additional context in comments.

5 Comments

Are you sure it's the checkFirebase ref upon which you'd call off and not the ref returned by limitToLast?
if you call off on checkFirebase, it will stop listening for any child_added in checkFirebase reference, Do you want that or something else?
It's just that that's not the reference upon which on was called - as limitToLast returns a reference with the limit applied. Doesn't look right, that's all.
limitToLast is just a listener to the last 20 child, if there is new child in last 20, it will fire the callback function. So if you want to only listen for child_added in last 20 child, you should call off with limitToLast
According to official firebase documentation, it should have worked, If a callback is not specified, all callbacks for the specified eventType will be removed. Similarly, if no eventType or callback is specified, all callbacks for the Reference will be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.