Using .numChildren() in AngularFire

末鹿安然 提交于 2019-12-13 08:59:49

问题


I'm trying to use .numChildren() in AngularFire, but not sure I'm doing it correctly.

function getServiceProviders(serviceId) {
  var serviceProviders = ref.child('services').child(serviceId).child('providers');
  return serviceProviders.numChildren();
}

I'm getting the following error:

TypeError: e.numChildren is not a function

Not sure if this is due to me using Browserify, or I'm just trying to access numChildren incorrectly.

Any help is appreciated. Thanks in advance!


回答1:


Your code snippet doesn't use AngularFire, it only uses the Firebase JavaScript SDK. Although your project undoubtedly uses AngularFire, it doesn't relate to this question.

When you look at the documentation for the .child() method in the Firebase JavaScript SDK, you'll see that it returns a Firebase reference. And if you look further into that class, you should notice that it doesn't have a numChildren method.

numChildren is only available on a DataSnapshot object, which you get in any of the on(... event handlers.

So:

serviceProviders.on('value', function(snapshot) {
  console.log(snapshot.numChildren());
}

Since the snapshot will be loaded asynchronously, you cannot return the number of children from getServiceProviders. See my answer to this question for a broader explanation of that: Asynchronous access to an array in Firebase



来源:https://stackoverflow.com/questions/29849494/using-numchildren-in-angularfire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!