2015-01-07 18 views
1

正如標題所說,我得到的錯誤是:灰燼斷言失敗:從findQuery的反應必須是一個數組,而不是不確定的

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

我檢查每一個SO回答(像this onethis one)我可以找到像我一樣的類似錯誤,但所提供的解決方案都不能幫助我解決問題。

我正在學習Ember並嘗試使用Ember Data RESTAdapter和RESTSerializer來修復來自OMDB API的JSON響應。

我得到的錯誤的大部分答案都會提示一個格式錯誤的JSON(例如uncamelized屬性名稱),但我很肯定沒有任何錯誤,我如何序列化JSON在MovieSerializer.extractArray

我曾嘗試加入normalize方法以及normalizeHash但是,如前所述,我無法找到一個屬性,它是無效的或丟失(id有)需要被標準化。

JSON接收:

{ 
    "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" 
} 

序列化JSON(記錄在標記的代碼部分波紋管)

{ 
    "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" 
     } 
    ] 
} 

我的應用程序的有關代碼是:

模型

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

指數路線

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

適配器

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

}); 

串行

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

回答

1

好的,所以我找到了錯誤源。我忘了返回extractArray的數組。

我所做的更改很簡單:

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

還有一點要注意,其他人在關注這個問題。不要覆蓋像normalizeextractArray這樣的內置鉤子,並且確保滿足這些鉤子的必要條件(如返回值,請致電super等)

+0

對不起,我得到了同樣的錯誤,新的EmberJS。我應該在哪裏進行修改?爲什麼?使用燼1.10。謝謝。 – Soullivaneuh

+0

它在我的答案中說得很對。你改變'extractArray'方法來將調用返回給'_super'。原因是'extractArray'被其他Ember函數用來轉換你從服務器得到的數組,所以Ember希望該方法返回一個數組。 –

相關問題