【问题标题】:Unable to pass result of a query to view and render into JSON无法传递查询结果以查看并呈现为 JSON
【发布时间】:2015-05-09 16:23:17
【问题描述】:

我正在尝试查询 Parse 表,并且能够获取数据。然后我将该数据传递给 Parse View 以呈现到车把模板中。当我手动将 JSON 数据输入到集合变量中时,视图本身正在工作。

我的问题是数据没有从查询传递到视图或没有转换为 JSON。我不断收到以下错误:

未捕获的类型错误: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);
} 
});  

我做错了什么?

【问题讨论】:

  • 我刚刚注意到,问题不在于将数据从查询传递到视图。因为当我更改 'code'var collection = { storeList: this.collection };然后我得到一个数组中的数据。

标签: javascript json parse-platform handlebars.js


【解决方案1】:

可能是您正在编码而不是解码?另外,我会检查this.collection的类型,尤其是this。因为Backbone.toJSON() 适用于模型。

例如。使用JSON.stringify()进行编码,将对象转换为json字符串以存储在数据库中,并在使用JSON.parse()从数据库检索后进行解码

Backbone uses .toJSON()this post explains 进一步介绍骨干网中的序列化/反序列化

【讨论】:

  • 太棒了,谢谢。我认为它不起作用,因为我试图在不包含backbone.js的情况下使用该功能。您的链接有所帮助,它现在可以一起使用这些解析和字符串化。 var collection = { storeList: JSON.parse(JSON.stringify(this.collection)) };
  • 太棒了!很高兴这有帮助。是的。记住编码和解码:)在你的情况下,两者都是!
  • 如果有帮助请点赞!因为它在序列化方面具有价值干杯
  • 我想要,但我似乎没有足够的声望点。
猜你喜欢
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-26
相关资源
最近更新 更多