简体   繁体   English

使用Javascript中断Firebase中的循环

[英]Break loop in Firebase with Javascript

I am a beginner Firebase and am having trouble breaking the loop. 我是Firebase的初学者,无法打破循环。 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;
        }

    }

});

If you want to stop listening the child_added event, you can try Off . 如果要停止收听child_added事件,可以尝试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 有关off更多信息,请阅读firebase References#off文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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