Using backbone.js and trying to get data from postsList
array I got this in chrome console.log
d {length: 0, models: Array[0], _byId: Object, _byCid: Object, constructor: function…}
_byCid: Object
_byId: Object
length: 9
models: Array[9]
__proto__: f
When I'm trying to use console.log(postsList.length)
I get 0, but there are 9 models inside. I don't know how to get number of them.
The Size of the different collections can be found with the size() method. This method returns the number of elements in this collection.
count() method is used to return the count of documents that would match a find() query. The db. collection. count() method does not perform the find() operation but instead counts and returns the number of results that match a query.
MongoDB count() Method – db. Collection. count() The count() method counts the number of documents that match the selection criteria.
Yes, this is strange behaviour :)
Chrome displays object preview immediately after you have used console.log
.
When you entered console.log(collection)
it was empty (probably you have fetched model from the server). But at the moment when you expand the object in console Chrome displays actual object params at current moment.
Try this in console:
var object = {key1:{prop:true}};
console.log(object)
object.key2 = true;
console.log(object)
To get collection length use this way:
collection.fetch({context:collection}).done(function() {
console.log(this.length)
});
No-no-no :)
Use this.length
instead of this.lenght
.
collection.fetch({context:collection}).done(function() {
// equals to this.length
console.log(this.size());
// get all models from collection (array of Backbone.Models)
console.log(this.models);
// get all models from collection (like simple array of objects)
console.log(this.toJSON());
// get model with index 1
console.log(this.at(1));
// get model data with index 1
console.log(this.at(1).toJSON());
// get model with id `some-id`
console.log(this.get('some-id'));
// get models data where property `id_str` equals to `292724698935070722`
console.log(this.where({id_str:'292724698935070722'}));
});
For more information look here: http://backbonejs.org/#Collection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With