1

As the title says, the error I am getting is:

Error while processing route: index Assertion Failed: The response from a findQuery must be an Array, not undefined

I checked every single SO answer (like this one or this one) I could find with similar error like mine and none of the solutions provided helped me solve my problem.

I am learning Ember and trying to mess around with Ember Data RESTAdapter and RESTSerializer to fix up the JSON response from OMDB API.

Most answers to the error I am getting suggest a malformed JSON (such as uncamelized property names) but I am pretty sure nothing is wrong with how I serialize the JSON in MovieSerializer.extractArray.

I have tried adding the normalize method as well as normalizeHash but, as mentioned, I cannot find a property that is invalid or missing (id is there) that needs to be normalized.

JSON received:

{
    "Title": "Pulp Fiction",
    "Year": "1994",
    "Rated": "R",
    "Released": "14 Oct 1994",
    "Runtime": "154 min",
    "Genre": "Crime, Drama, Thriller",
    "Director": "Quentin Tarantino",
    "Writer": "Quentin Tarantino (story), Roger Avary (story), Quentin Tarantino",
    "Actors": "Tim Roth, Amanda Plummer, Laura Lovelace, John Travolta",
    "Plot": "The lives of two mob hit men, a boxer, a gangster's wife, and a pair of diner bandits intertwine in four tales of violence and redemption.",
    "Language": "English, Spanish, French",
    "Country": "USA",
    "Awards": "Won 1 Oscar. Another 63 wins & 47 nominations.",
    "Poster": "http://ia.media-imdb.com/images/M/MV5BMjE0ODk2NjczOV5BMl5BanBnXkFtZTYwNDQ0NDg4._V1_SX300.jpg",
    "Metascore": "94",
    "imdbRating": "8.9",
    "imdbVotes": "1,039,031",
    "imdbID": "tt0110912",
    "Type": "movie",
    "Response": "True"
}

Serialized JSON (logged in the marked code portion bellow)

{
    "movies": [
        {
            "id": 1,
            "title": "Pulp Fiction",
            "year": "1994",
            "rated": "R",
            "released": "14 Oct 1994",
            "runtime": "154 min",
            "genre": "Crime, Drama, Thriller",
            "director": "Quentin Tarantino",
            "writer": "Quentin Tarantino (story), Roger Avary (story), Quentin Tarantino",
            "actors": "Tim Roth, Amanda Plummer, Laura Lovelace, John Travolta",
            "plot": "The lives of two mob hit men, a boxer, a gangster's wife, and a pair of diner bandits intertwine in four tales of violence and redemption.",
            "language": "English, Spanish, French",
            "country": "USA",
            "awards": "Won 1 Oscar. Another 63 wins & 47 nominations.",
            "poster": "http://ia.media-imdb.com/images/M/MV5BMjE0ODk2NjczOV5BMl5BanBnXkFtZTYwNDQ0NDg4._V1_SX300.jpg",
            "metascore": "94",
            "imdbRating": "8.9",
            "imdbVotes": "1,039,031",
            "imdbId": "tt0110912",
            "type": "movie",
            "response": "True"
        }
    ]
}

The relevant code of my app is:

Model

var Movie = DS.Model.extend({
    title:      DS.attr('string'), 
    year:       DS.attr('string'),      
    rated:      DS.attr('string'),
    released:   DS.attr('string'),
    runtime:    DS.attr('string'),
    genre:      DS.attr('string'),
    director:   DS.attr('string'),
    writer:     DS.attr('string'),
    actors:     DS.attr('string'),
    plot:       DS.attr('string'),
    language:   DS.attr('string'),
    country:    DS.attr('string'),
    awards:     DS.attr('string'),
    poster:     DS.attr('string'),
    metascore:  DS.attr('string'),
    imdbRating: DS.attr('string'),  
    imdbVotes:  DS.attr('string'),
    imdbId:     DS.attr('string'),
    type:       DS.attr('string'),
    response:   DS.attr('string')
});

Index Route

var IndexRoute = Ember.Route.extend({
    model: function() {
        return this.get('store').find('movie', {title: 'Pulp Fiction'}); // calls findQuery in the RESTAdapter
    }
});

Adapter

var MovieAdapter = DS.RESTAdapter.extend({

    // request sent to http://www.omdbapi.com/?t=pulp+fiction&y=&plot=short&r=json
    buildURL: function(item) {
        var title = item.title.trim().replace(/\s+/, '+').replace(/[A-Z]/g, function(val) {
            return val.toLowerCase();
        });
        return "http://www.omdbapi.com/?t=" + title + "&y=&plot=short&r=json";
    }

    findQuery: function(store, type, query) {
        return this.ajax(this.buildURL(query), 'GET');
    }

});

Serializer

var MovieSerializer = DS.RESTSerializer.extend({

    extractArray: function(store, type, payload) {
        var movies = [{
            id: 1 // hard-code an id for now
        }];

        var camelKey;
        for(var key in payload) {
            camelKey = Ember.String.decamelize(key).camelize();
            movies[0][camelKey] = payload[key];
        }           

        payload = { movies: movies };
        console.log(JSON.stringify(payload)); // THE SERIALIZED JSON ABOVE IS LOGGED AT THIS POINT
        this._super(store, type, payload);
    }
});
Community
  • 1
  • 1
Bug Feature
  • 21
  • 1
  • 7

1 Answers1

1

OK, so I found the error source.

I forgot to return the array in extractArray.

The change I made is simply:

extractArray: function(store, type, payload) {
    // ...
    return this._super(store, type, payload); // added this return statement
}

One more thing to note for other people looking at this question. Don't overwrite built-in hooks like normalize or extractArray without specific need and make sure to satisfying the necessary condition of those hooks (such as return value, calling super etc.)

Bug Feature
  • 21
  • 1
  • 7
  • Sorry, I got the same error and I'm new on EmberJS. Where should I put this modification? And Why? Using ember 1.10. Thanks. – Soullivaneuh Mar 20 '15 at 10:15
  • It says it right in my answer. You change the method `extractArray` to return the call to `_super`. The reason is that `extractArray` is used by other Ember functions to transform the array your got from a server so Ember expects the method to return an array. – Bug Feature Mar 31 '15 at 19:37