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"