2016-03-08 137 views
0

我想通过一个属性,使用此代码搜索对象:搜索对象的火力地堡

   ref.orderByChild("aFieldNameOnTheFirebaseCollection").equalTo(mySearchArgument).limitToFirst(1).on("child_added", function(snapshot) { 
       console.log("Yes the object with the key exists !"); 
       var thisVerificationVarisSetToTrueIndicatingThatTheObjectExists = true ; 
      }); 

如果一个或多个对象的集合中没有找到这一工程确定。但是,我需要知道是否没有对象存在。在验证之前,我可以将验证变量设置为false,但验证过程是异步的,我需要等待完成。我使用承诺?

回答

2

A child_added如果(且仅在)儿童被添加时,事件才会触发。所以你不能用它来检测一个匹配的孩子是否存在。

使用一个value事件:

var query = ref.orderByChild("aFieldNameOnTheFirebaseCollection").equalTo(mySearchArgument).limitToFirst(1); 
query.on("value", function(snapshot) { 
    if (snapshot.hasChildren()) { 
    console.log("Yes the object with the key exists !"); 
    var thisVerificationVarisSetToTrueIndicatingThatTheObjectExists = true ; 
    } 
}) 
+0

完美。非常感谢你 ! – GCoe