获取由firebase返回的数组的长度 [英] Get length of an array returned by firebase

查看:104
本文介绍了获取由firebase返回的数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数组的常用.length属性对firebase返回的数组无效。

  0:Object 
1:Object
2这里是返回数组的console.log:

:Object
3:Object
4:Object
$$ added:function(){[native code]}
$$ error:function(){[native code]}函数(){[本机代码]}
$$已删除:函数(){[本机代码]}
$$已更改: }
$ add:function(){[native code]}
$ destroy:function(){[native code]} $ b $ getRecord:function(){[native code]}
$ indexFor:function(){[native code]}
$ inst:function(){[native code]}
keyAt:function(){[native code]} $ b $函数(){[原生代码]}
$删除:函数(){[原生代码]}
$保存:函数(){[原生代码]}
$注意:function(){[native code]}
length:5
__proto__:Array [0]
concat:function concat(){[native code]}
构造函数:函数Array(){[native code]}
every:function every(){[native code]}
f如果()函数过滤器(){[本机代码]}
findIndex:函数(一){如果(null ==这个)抛出新的TypeError(Array.prototype.find调用null或undefined); function!= typeof a)throw new TypeError(predicate must be a function); for(var b,c = Object(this),d = c.length>>> 0,e = arguments [1 (b,c,f),a.call(e,b,f,c)))返回f; return-1} $(f,f = 0; d> f; f ++) b $ forEach:function forEach(){[native code]}
indexOf:function indexOf(){[native code]}
join:function join(){[native code]}
lastIndexOf:function lastIndexOf(){[native code]}
length:0
map:function map(){[native code]}
pop:function pop(){[native code ]}
push:function push(){[native code]}
reduce:function reduce(){[native code]}
reduceRight:function reduceRight(){[native code]}
reverse:function reverse(){[native code]}
shift:function shift(){[native code]}
slice:function slice(){[native code]}
一些:功能函数splice(){[native code]}
toLocaleString:function toLocaleString() ){[native code]}
toString:function toString(){[native code]}
unshift:function unshift(){[native code]}



甚至console.log的长度属性显示5,但由于某种原因调用长度属性返回0。



数组的length属性被设置为5,但是 proto的长度属性 >是0. .length属性返回 proto 下的0值。记住调用 $ asArray 是一个异步操作。

  $ scope.notes = $ firebase(ref)。$ asArray(); 
console.log($ scope.notes.length); //数据尚未加载

您在控制台中看到零长度的原因是因为值可以在日志缓冲和打印之间改变,这可能是非常具有误导性的。因为数据是异步的,所以你需要使用 $ loaded c $ c> promise。

  $ scope.notes。$ loaded()。then(function(notes){
console.log(notes.length); //数据在这里加载
});


The usual .length property of the array doesn't work on the arrays returned by firebase. Here is the console.log of the returned array:

0: Object
1: Object
2: Object
3: Object
4: Object
$$added: function () { [native code] }
$$error: function () { [native code] }
$$moved: function () { [native code] }
$$removed: function () { [native code] }
$$updated: function () { [native code] }
$add: function () { [native code] }
$destroy: function () { [native code] }
$getRecord: function () { [native code] }
$indexFor: function () { [native code] }
$inst: function () { [native code] }
$keyAt: function () { [native code] }
$loaded: function () { [native code] }
$remove: function () { [native code] }
$save: function () { [native code] }
$watch: function () { [native code] }
length: 5
__proto__: Array[0]
concat: function concat() { [native code] }
constructor: function Array() { [native code] }
every: function every() { [native code] }
filter: function filter() { [native code] }
findIndex: function (a){if(null==this)throw new TypeError("Array.prototype.find called on null or undefined");if("function"!=typeof a)throw new TypeError("predicate must be a function");for(var b,c=Object(this),d=c.length>>>0,e=arguments[1],f=0;d>f;f++)if(f in c&&(b=c[f],a.call(e,b,f,c)))return f;return-1}
forEach: function forEach() { [native code] }
indexOf: function indexOf() { [native code] }
join: function join() { [native code] }
lastIndexOf: function lastIndexOf() { [native code] }
length: 0
map: function map() { [native code] }
pop: function pop() { [native code] }
push: function push() { [native code] }
reduce: function reduce() { [native code] }
reduceRight: function reduceRight() { [native code] }
reverse: function reverse() { [native code] }
shift: function shift() { [native code] }
slice: function slice() { [native code] }
some: function some() { [native code] }
sort: function sort() { [native code] }
splice: function splice() { [native code] }
toLocaleString: function toLocaleString() { [native code] }
toString: function toString() { [native code] }
unshift: function unshift() { [native code] }

even the console.log length property shows 5 but for some reason calling the length property returns 0.

Edit:

The length property of the array is set to 5 but the length property of the proto is 0. The .length property returns the 0 value under the proto .

解决方案

Keep in mind that calling $asArray is an asynchronous action.

$scope.notes = $firebase(ref).$asArray();
console.log($scope.notes.length); // the data has not loaded yet

The reason why you're seeing a zero length in the console is because values can change between buffering of the log and printing it, which can be very misleading. Try using a breakpoint to see the values as they execute.

Since the data is asynchronous you'll need to use the $loaded promise.

$scope.notes.$loaded().then(function(notes) {
   console.log(notes.length); // data is loaded here
});

这篇关于获取由firebase返回的数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆