0

I'm creating an application with ionic and firebase. I'm trying to verify if a element exists in my array, and if it does, I need to return true, else I need to return false. The problem is, it always return false, even if the item exists in firebase. Can you please tell me what is going wrong with following code?

Here's my service:

function IsReserved(id){
    var ref = fb.child('/reserved/').orderByChild('product').equalTo(id);
    ref.once("value").then(function(snapshot){
        snapshot.forEach(function(data){
            if(data.val().user === $rootScope.currentUser.$id){
                console.log(data.val().user + " * " + $rootScope.currentUser.$id);
                return true;
            }
        });
    });

    return false;
}

Here is my controller:

function Reservar(produto) {
if(!$rootScope.cart){
  $rootScope.cart = [];
  $rootScope.fprice = 0;
}

var user=$rootScope.currentUser;
var res = vm.IsReserved(produto.$id);
console.log(res);


if(res){
    console.log("já reservado");                
    return;
}

Here is my firebase strucure:

-reserved:
     --KdS2cH1OJ5MhKAV6Yio:
            -product: "product1"
            -user: "W30BB1RMg1XhNo9og9cMo4Gpr4S2"
2
  • Please post a snippet of your Firebase structure, as text please, no images. Commented Feb 21, 2017 at 13:01
  • -reserved --KdS2cH1OJ5MhKAV6Yio -product: "produtc1" -user: "W30BB1RMg1XhNo9og9cMo4Gpr4S2" Commented Feb 21, 2017 at 13:03

1 Answer 1

2

Your code won't work because firebase works asynchronously.

You should use a callback function as a parameter, something like this:

function IsReserved(id, callback){
            var ref = fb.child('/reserved/').orderByChild('product').equalTo(id);
            ref.once("value").then(function(snapshot){
                snapshot.forEach(function(data){
                    if(data.val().user === $rootScope.currentUser.$id){
                        console.log(data.val().user + " * " + $rootScope.currentUser.$id);
                        callback(true);
                        return;
                    }
                });
            });
            
            return false; //-- This will always be executed before the code inside the .then, that's why your function always returns false
        }

And on you controller, something like this:

function Reservar(produto)
        {
            if(!$rootScope.cart){
              $rootScope.cart = [];
              $rootScope.fprice = 0;
            }

            var user=$rootScope.currentUser;
            vm.IsReserved(produto.$id, function(response){
               console.log(response);
               if(response){
                console.log("já reservado");                
               }
            });
  }
            

Could you understand?

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

3 Comments

The source works, But the forEach needs to break and it return true. Your source doesn't brake, any idea? (Are you BR?)
@LucasGaspar, you should comment/remove return false and if(data.val().user === $rootScope.currentUser.$id) should return 1 record not multiple, if not, then the code must be rewrite to add break in forloop
@LucasGaspar Sim, BR! I think you can just add a return; after calling the callback inside the forEach.

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.