Angularjs 在Firebase上搜索对象

Angularjs 在Firebase上搜索对象,angularjs,firebase,Angularjs,Firebase,我正在尝试使用以下代码按属性搜索对象: ref.orderByChild("aFieldNameOnTheFirebaseCollection").equalTo(mySearchArgument).limitToFirst(1).on("child_added", function(snapshot) { console.log("Yes the object with the key exists !");

我正在尝试使用以下代码按属性搜索对象:

                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,但是验证过程是异步的,我需要等待它完成。我应该使用承诺吗?

a
child\u added
事件将在(且仅当)添加了一个孩子时触发。因此,您不能使用它来检测是否存在匹配的子级

使用
事件:

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 ;
  }
})

如果(且仅当)添加了子项,则将触发添加的子项事件。因此,您不能使用它来检测是否存在匹配的子级

使用
事件:

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 ;
  }
})

完美的非常感谢你!完美的非常感谢你!