本文介绍了在AngularFire中使用.numChildren()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在AngularFire中使用.numChildren(),但不确定我是否正确执行了操作.

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

我遇到以下错误:

TypeError: e.numChildren is not a function

不确定这是否是由于我使用Browserify造成的,还是我只是尝试错误地访问numChildren.

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!

推荐答案

您的代码段不使用AngularFire,而仅使用Firebase JavaScript SDK.尽管您的项目无疑使用了AngularFire,但它与该问题无关.

当您查看.child()方法中的文档时Firebase JavaScript SDK ,您会看到它返回了 Firebase参考.而且,如果进一步研究该类,您应该注意到它没有numChildren方法.

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 仅在 DataSnapshot 对象,您可以在任何on(...事件中获得该对象处理程序.

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

所以:

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

由于快照将异步加载,因此无法从getServiceProviders返回子代数.请参阅我对这个问题的答案,以得到更广泛的解释:异步在Firebase中访问数组

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

这篇关于在AngularFire中使用.numChildren()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:25