断言失败:#each循环的值必须是数组。你通过了[object Object] [英] Assertion failed: The value that #each loops over must be an Array. You passed [object Object]

查看:110
本文介绍了断言失败:#each循环的值必须是数组。你通过了[object Object]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按下按钮时,我正在尝试在运行时更改模板的模型。为了更改称为示例的模板模型,在按钮按下时调用changeModel动作。这个方法会产生一个Ajax请求,并从后端获取一些数据。现在的问题是应用程序转换到示例模板,但新的数据不会显示在模板中。我验证了数据是从服务器发送的。当我将完全相同的数据放入一个变量,并尝试将其加载为一个模型,它的工作原理。可能是什么问题?是问题,因为在转换到新页面之前,Ajax请求没有解决,我需要以某种方式使用promise?还是要发送的数据的数据类型不同?如果是这样,我该怎么把它转换成一个数组。这个问题与我之前提到的另外一个问题有关:在Ember.js中渲染模板并调用模型钩子。你可以看看我正在使用的代码。

I am trying to change the model for a template at runtime when a button is pressed. For changing the model of template called 'example', changeModel action is called on button press. This method makes an Ajax request and gets some data from the backend. Now the problem is that the app transitions to the example template but the new data is not displayed in the template. I verified that the data is being sent from the server. When I put exactly same data into a variable and try to load that as a model, it works. What might be the problem? Is the problem because the Ajax request is not resolved before transitioning to the new page and I need to use promise somehow? Or is the data type of data being sent different? If so, how can I convert it into an array. This question is in a way linked to the other question I asked a few days back: Rendering a template and invoking model hook in Ember.js. You can have a look at the code I am using.

changeModel: function(data) {
    var url = "/ChangeModel";
    var newModel= Ember.$.getJSON(url).then(function(data) {
      return data;
    });
    var self = this;
    this.transitionTo('example').then(function(){
        self.controllerFor('example').set('model', newModel);
    });
 }


推荐答案

可变的newModel接缝作为一个承诺。

The varible 'newModel' seams to be a promise.

尝试这样:替换你的代码:

Try this: replace your code:

self.controllerFor('example').set('model', newModel);

newModel.then(
  function(data) {
    self.controllerFor('example').set('model', data);
});

这篇关于断言失败:#each循环的值必须是数组。你通过了[object Object]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆