Javascript 成员断言失败:来自findQuery的响应必须是数组,而不是未定义的
正如标题所说,我得到的错误是:Javascript 成员断言失败:来自findQuery的响应必须是数组,而不是未定义的,javascript,json,api,rest,ember.js,Javascript,Json,Api,Rest,Ember.js,正如标题所说,我得到的错误是: Error while processing route: index Assertion Failed: The response from a findQuery must be an Array, not undefined 我检查了每一个答案(如或),这样我就可以找到与我类似的错误,并且提供的任何解决方案都不能帮助我解决问题 我正在学习Ember,并尝试使用Ember数据RestaAdapter和RESTSerializer来修复来自的JSON响应 我收
Error while processing route: index Assertion Failed: The response from a findQuery must be an Array, not undefined
我检查了每一个答案(如或),这样我就可以找到与我类似的错误,并且提供的任何解决方案都不能帮助我解决问题
我正在学习Ember,并尝试使用Ember数据RestaAdapter和RESTSerializer来修复来自的JSON响应
我收到的错误的大多数答案都表明JSON格式错误(例如未命名的属性名),但我非常确定我在MovieSerializer.extractArray
中序列化JSON的方式没有问题
我已经尝试添加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(记录在下面标记的代码部分)
我的应用程序的相关代码是:
型号
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);
}
});
序列化程序
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);
}
});
好的,我找到了错误源 我忘了在
extractArray
中返回数组
我所做的改变很简单:
extractArray: function(store, type, payload) {
// ...
return this._super(store, type, payload); // added this return statement
}
还有一件事需要注意,其他人在看这个问题。不要在没有特定需要的情况下覆盖内置钩子,如
normalize
或extractArray
,并确保满足这些钩子的必要条件(例如返回值、调用super
等)对不起,我遇到了同样的错误,我是新加入EmberJS的。我应该把这个修改放在哪里?为什么?使用余烬1.10。谢谢,我的答案说得对。您可以更改方法extractArray
,将调用返回到\u super
。原因是其他Ember函数使用extractArray
转换从服务器获取的数组,因此Ember希望该方法返回数组。