简体   繁体   中英

Unable to pass result of a query to view and render into JSON

I am trying to query a Parse table and I am able to get the data out. I am then passing that data to a Parse View to be rendered into a handlebar template. The view in itself is working when I manually input JSON data into the collection variable.

My problem is that the data is not getting passed to the View from the query or not getting converted to JSON. I keep getting the following error:

Uncaught TypeError: this.collection.toJSON is not a function

// render template with context data 
var StoresView =  Parse.View.extend({
template: Handlebars.compile($('#storetable-tpl').html()),
render: function(){ 
    var collection = { storeList: this.collection.toJSON() };
    this.$el.html(this.template(collection));
}
});

// query the store table data
allStores.equalTo("shape","Round");
allStores.limit(10);
allStores.descending("updatedAt");
allStores.find({
success: function(results) {
    var storesView = new StoresView({ collection: results });
    storesView.render();
    $('#stores-table').html(storesView.el);
} 
});  

What am I doing wrong?

Could it be that you are encoding, but not decoding? Also, I will check the type of this.collection , especially this . Since Backbone.toJSON() applies to models.

ex. to encode using JSON.stringify() for converting objects to json string to store in db and to decode when after retrieving from db using JSON.parse()

Backbone uses .toJSON() and this post explains further on serialization/deserialzation in backbone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM