我尝试添加 normalize 方法以及 normalizeHash 但是,如上所述,我找不到无效或丢失的属性 (id代码>在那里)需要规范化.收到的 JSON:{标题":低俗小说","年": "1994","额定": "R",发布":1994 年 10 月 14 日","运行时间": "154 分钟",类型":犯罪,戏剧,惊悚",《导演》:《昆汀·塔伦蒂诺》,作家":昆汀·塔伦蒂诺(故事),罗杰·阿瓦里(故事),昆汀·塔伦蒂诺",演员":蒂姆·罗斯、阿曼达·普卢默、劳拉·洛夫莱斯、约翰·特拉沃尔塔"、剧情":两个暴徒的生活,一个拳击手,一个黑帮的妻子,和一对餐馆强盗的生活,交织在四个暴力和救赎的故事中.","Language": "英语、西班牙语、法语","国家": "美国",奖项":获得了 1 项奥斯卡奖.另外 63 项获奖和 47 项提名.","海报": "http://ia.media-imdb.com/images/M/MV5BMjE0ODk2NjczOV5BMl5BanBnXkFtZTYwNDQ0NDg4._V1_SX300.jpg","Metascore": "94","imdbRating": "8.9","imdbVotes": "1,039,031","imdbID": "tt0110912","类型": "电影",回应":真实"}序列化的 JSON(记录在下面标记的代码部分){电影": [{身份证":1,"title": "低俗小说","年": "1994","额定": "R","released": "14 Oct 1994","运行时间": "154 分钟",流派":犯罪,戏剧,惊悚",导演":昆汀塔伦蒂诺",作家":昆汀·塔伦蒂诺(故事),罗杰·阿瓦里(故事),昆汀·塔伦蒂诺",演员":蒂姆·罗斯、阿曼达·普卢默、劳拉·洛夫莱斯、约翰·特拉沃尔塔"、情节":两个暴徒杀手的生活,一个拳击手,一个黑帮的妻子,和一对餐馆强盗的生活,交织在四个暴力和救赎的故事中.","language": "英语、西班牙语、法语","国家": "美国","awards": "获得了 1 项奥斯卡奖.另外 63 项获奖和 47 项提名.","海报": "http://ia.media-imdb.com/images/M/MV5BMjE0ODk2NjczOV5BMl5BanBnXkFtZTYwNDQ0NDg4._V1_SX300.jpg","metascore": "94","imdbRating": "8.9","imdbVotes": "1,039,031","imdbId": "tt0110912","类型": "电影",回应":真实"}]}我的应用的相关代码是:模型var Movie = DS.Model.extend({标题:DS.attr('string'),年份:DS.attr('string'),评级:DS.attr('string'),发布:DS.attr('string'),运行时:DS.attr('string'),流派:DS.attr('string'),导演:DS.attr('string'),作者:DS.attr('string'),演员:DS.attr('string'),情节:DS.attr('string'),语言:DS.attr('string'),国家:DS.attr('string'),奖项:DS.attr('string'),海报:DS.attr('string'),元评分:DS.attr('string'),imdbRating: DS.attr('string'),imdbVotes: DS.attr('string'),imdbId: DS.attr('string'),类型:DS.attr('string'),响应:DS.attr('string')});索引路线var IndexRoute = Ember.Route.extend({模型:函数(){return this.get('store').find('movie', {title: '低俗小说'});//调用 RESTAdapter 中的 findQuery}});适配器var MovieAdapter = DS.RESTAdapter.extend({//请求发送到 http://www.omdbapi.com/?t=pulp+fiction&y=&plot=short&r=jsonbuildURL:功能(项目){var title = item.title.trim().replace(/\s+/, '+').replace(/[A-Z]/g, function(val) {返回 val.toLowerCase();});返回 "http://www.omdbapi.com/?t=" + 标题 + "&y=&plot=short&r=json";}findQuery:函数(存储,类型,查询){返回 this.ajax(this.buildURL(query), 'GET');}});序列化器var MovieSerializer = DS.RESTSerializer.extend({提取数组:函数(存储,类型,有效载荷){var 电影 = [{id: 1//现在硬编码一个 id}];var骆驼键;for(有效载荷中的var键){camelKey = Ember.String.decamelize(key).camelize();电影[0][camelKey] = 有效载荷[key];}有效载荷 = { 电影:电影 };console.log(JSON.stringify(payload));//上面的序列化 JSON 在这一点上被记录this._super(store, type, payload);}}); 解决方案 好的,我找到了错误源.我忘记在 extractArray 中返回数组.我所做的改变很简单:extractArray: function(store, type, payload) {//...返回 this._super(store, type, payload);//添加这个返回语句}对于看这个问题的其他人,还有一件需要注意的事情.不要在没有特定需要的情况下覆盖诸如 normalize 或 extractArray 之类的内置钩子,并确保满足这些钩子的必要条件(例如返回值、调用 超级等)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 undefinedI 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:Modelvar 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 Routevar IndexRoute = Ember.Route.extend({ model: function() { return this.get('store').find('movie', {title: 'Pulp Fiction'}); // calls findQuery in the RESTAdapter }});Adaptervar 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'); }});Serializervar 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); }}); 解决方案 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.) 这篇关于Ember 断言失败:来自 findQuery 的响应必须是一个数组,而不是未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 18:29