简体   繁体   English

无法传递查询结果以查看并呈现为JSON

[英]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. 我正在尝试查询一个Parse表,并且能够获取数据。 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. 当我手动将JSON数据输入到collection变量中时,视图本身正在工作。

My problem is that the data is not getting passed to the View from the query or not getting converted to JSON. 我的问题是数据没有从查询传递到视图或没有转换为JSON。 I keep getting the following error: 我不断收到以下错误:

Uncaught TypeError: this.collection.toJSON is not a function 未捕获的TypeError:this.collection.toJSON不是函数

// 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 . 另外,我将检查this.collection的类型,尤其是this Since Backbone.toJSON() applies to models. 由于Backbone.toJSON()适用于模型。

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() 使用JSON.stringify()进行编码以将对象转换为json字符串以存储在数据库中,并在使用JSON.parse()从数据库中检索后进行解码

Backbone uses .toJSON() and this post explains further on serialization/deserialzation in backbone 骨干网使用.toJSON()这篇文章进一步解释了骨干网中的序列化/反序列化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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