Get length of array returned by firebase

The usual .length property of an array does not work with arrays returned by firebase. Here is the .log console 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 length property of console.log shows 5, but for whatever reason, calling the length property returns 0.

Edit:

The length property of the array is 5, but the length property of proto is 0. The .length property returns 0 under proto .

+3


source to share


1 answer


Be aware that the call $asArray

is an asynchronous action.

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

      

The reason you see zero length in the console is because the values ​​can change between log buffering and printing, which can be very erroneous. Try using a breakpoint to see the values ​​as they run.



Since the data is asynchronous, you need to use a promise $loaded

.

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

      

+21


source







All Articles