Assertion failed: The value that #each loops over must be an Array. You passed [object Object]

隐身守侯 提交于 2019-12-14 02:24:39

问题


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);
    });
 }

回答1:


The varible 'newModel' seams to be a promise.

Try this: replace your code:

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

with

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


来源:https://stackoverflow.com/questions/21691927/assertion-failed-the-value-that-each-loops-over-must-be-an-array-you-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!