2016-03-08 134 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