EmberJS - Error: The item being looped over in #each must be an Array. You provided (application controller)

I'm having trouble displaying my model's fixture data on the template. When I attempt to use an each loop in the template, I encounter the error mentioned above:

{{#each}}
    {{title}}
{{/each}}

This is how I've set up my router:

Application.Router.map(function() {
    this.resource('application', { path: '/' });
});

Application.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('applicationmodel');
    }
});

Here is how my model looks:

Application.ApplicationModel = DS.Model.extend({
    title: DS.attr('string')
});

Application.ApplicationModel.FIXTURES = [
    {
        id: 1,
        title: 'title-1'
    },
    {
        id: 2,
        title: 'title-2'
    }
];

Could someone point out where I might be going wrong?

Appreciate any help!

Answer №1

Give this a try:

{{#each content}}
   {{title}}
{{/each}}

Additionally, you can use the following code snippets:

App.ApplicationController = Ember.ArrayController.extend({}) 

Application.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('applicationModel');
    },
    setupController: function(controller, model) {
        controller.set('content', model);
    }
});

UPDATE: Providing more information as requested in @Andy Hayden's comment:

The error message (

EmberJS - Assertion failed: The value that #each loops over must be an Array. You passed (generated application controller)
) indicates two key points:

  1. The item being looped over is not an Array. It appears we are looping through the content property of our controller, hinting at a missing ArrayController and using an ObjectController instead. This can be confirmed using the Ember Inspector

  2. Where did the controller originate from? Ember will automatically generate controllers if needed but not explicitly defined. The error message (generated application controller) confirms this auto generation process. By defining an ApplicationController as an ArrayController, we can control the type of controller being used rather than relying on Ember's generated ObjectController.

Answer №2

The reason for this error is that the controller being iterated in your template is not enumerable. Ember automatically creates an ApplicationController for you if you haven't defined one explicitly.

To resolve this, you can create an ApplicationController by using the following code:

App.ApplicationController = Ember.ArrayController.extend()

Answer №3

Have you found a solution to this issue? Are you currently utilizing Ember AppKit? I recently encountered a similar problem while working on an EAK project and stumbled upon Stack Overflow seeking help.

In our previous Ember applications (not using EAK), I would typically name my controller file as posts_controller.js, containing PostsController. However, with EAK, it seems to prefer the file name posts.js. If it's not named correctly, EAK won't return an ArrayController, causing issues in the template rendering process.

My colleague and I faced this same issue in different EAK projects. We were able to identify the root cause by observing that the console was generating a controller instead of loading the one we had specified.

Answer №4

In case the previous solutions don't work for you, it's possible that there was a typo in the template name or attribute of your handlebars script tag.

Double-check your index.html file to ensure that the <script> tag includes the correct attribute data-template-name="application"

The correct format should be:

<script type="text/x-handlebars" data-template-name="application">

Answer №5

App.Router.map(function() {
    this.resource('app', { path: '/' });
});

App.AppRoute = Ember.Route.extend({
    data: function() {
        return this.store.find('app');
    }
});

And the structure of my entity looks like this:

App.App = DS.Model.extend({
    name: DS.attr('string')
});


App.App.FIXTURES = [
    {
        id: 1,
        name: 'name-1'
    },
    {
        id: 2,
        name: 'name-2'
    }
];

Please implement the modifications mentioned above. In particular, delete 'data' from the names and ensure there is no space before FIXTURES.

{{#each application in model}}
     {{application.name}}
{{/each}}

Lastly, utilize model, as it represents the model property that you are iterating through.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript, where services are injected like Angular's dependency injection

I'm currently developing a Javascript npm package that consists of a single class resembling an angular service. The main goal is to ensure that only one instance of this class is created and can be shared throughout the project as needed. //The Shar ...

Ensure that a new window is opened while maintaining the exact location of the parent window

Having an issue with printing a calendar. The problem arises when trying to load CSS with absolute paths like /path/to/css.css. Opening a window with: var win = open('', '_blank') win.document.write(htmlContent) will display the HTML ...

How do I specify the content-type as application/json when using the DELETE method with $resource in AngularJS?

I am attempting to specify the content-type as application/json for the $resource delete action. The reason behind enforcing the content-type as application/json is due to an issue with IE10 and IE11, which recognize the content-type for DELETE requests as ...

Automatic closing of multile- vel dropdowns in Bootstrap is not enabled by default

I have successfully implemented bootstrap multilevel dropdowns. However, I am facing an issue where only one child is displayed at a time. <div class="container"> <div class="dropdown"> <button class="btn btn-default dropdown-to ...

Requiring Subclasses to Maintain Immutability

I have created a base class that contains various properties: class Component { readonly id: number readonly type: number } Now, I am looking to create some subclasses, such as: class HealthComponent extends Component { max_health: number, ...

Showing the initials of a user at the top of an SVG using ReactJS

https://i.sstatic.net/oJgXs.png I require the user's initials to be displayed on the avatars as a grey circle with those initials. I have the function ready, but I am unsure of how to implement it in the JSX code for the Dropdown menu (using Semantic ...

Having trouble with the Slide Menu Javascript functionality

Having some trouble creating a sliding menu with jQuery. Errors on lines 5 and 6 saying functions are not defined. Can anyone help me figure out what's wrong with my code? jQuery(function($) { "use strict"; $(document).ready(function () { ...

Is there a way to trigger the activation of the datepicker during the `onLoad` event?

For my project, I am utilizing this datepicker. While I am familiar with using scripts to handle changes in the date value, I am unsure of how to implement it on page load. $('.date_set .date').datepicker({ startView : 0, ...

Is it possible to exchange a JavaScript array with JSON content in a Search Application?

Greetings to all you fantastic individuals! I have developed an application where the script search.js searches through a JavaScript array to display a list of results when a user types in a keyword into a search bar and hits enter. However, I am now facin ...

What could be the reason for this JSON being considered "invalid"?

Despite passing validation on jsonlint, both Firefox and Chrome are rejecting this JSON: { "messages": [ { "subject": "One" }, { "subject": "Two" }, { "subject": "Three" ...

Transferring the jQuery modal variable value to a PHP script

I feel like I might be making this more complex than it needs to be, but I'm working on a jQuery modal that communicates with a PHP file. The PHP file contains the form and validation logic, and the modal includes this file. The modal is triggered by ...

Uploading Files Using JSON with Javascript and Jquery

I need to create an HTML list with a single li element named import. Behind it, there should be an input type ="file" that is initially hidden. When the user clicks on import, it should trigger the file upload from the hidden input field using .click(). ...

Searching for columns should be at the top of an angular datatable, not at the bottom

In my Angular 7 project, I am utilizing the library found at this link. I have followed the example provided, which can be seen here. Everything is working perfectly, except for the position of the search columns. I would like the search columns to appear ...

A guide on adding an onClick listener to a span element dynamically and then saving it to MongoDB

I have been attempting to add an onClick event within a span tag as shown below, with the goal of storing it in MongoDb. However, my event does not seem to be saving and is automatically removed. When I retrieve data from the database, it is not present. H ...

Efficiently flattening an array in JavaScript using recursive functions without the need for loops

Currently I am studying recursion and attempting to flatten an array without using loops (only recursion). Initially, I tried the iterative approach which was successful, but I am facing challenges with the pure recursive version: function flattenRecurs ...

Learn how to retrieve the ID of an element with a simple click using PHP, jQuery, AJAX, and Javascript

Apologies for being a newbie in this field, but I'm eager to learn. Any assistance would be greatly appreciated. In my project, there is a sidebar with rss links that I want to incorporate ajax functionality into. So, whenever a user clicks on a link ...

"Simply tap on an element that has been dynamically inserted into the

Many individuals are familiar with how to attach a "click" event to an element that is dynamically added using the following syntax: $('#main').on('click','.link',function(){ //some code here }); In this example, .link repr ...

Using Javascript to enable a button to perform multiple actions on an element

In my current setup, there are buttons that can adjust the height, color, and opacity of a box. For example- document.getelementbyID("growbutton").addEventlistener("click", function growFunction() { document.getelementbyID("box&q ...

Even after defining routes, Node Express continues to throw a 404 error

It appears that troubleshooting this issue may be challenging without providing more context. Here is the setup I have in a compact modular configuration: //index.js also known as the server ... // defining views path, template engine, and default layou ...

What could be causing the error when attempting to retrieve an index using the window variable?

I'm facing a strange issue where I am trying to utilize a variable that I define as follows: window.params['variable'] = { ... }; Within the function that I am using, the code looks like this: function q() { ... // for example return n ...