Ember Assertion Failed:findQuery的响应必须是Array,而不是未定义 [英] Ember Assertion Failed: The response from a findQuery must be an Array, not undefined

查看:165
本文介绍了Ember Assertion Failed:findQuery的响应必须是Array,而不是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,我得到的错误是:

 处理路由时出错:索引断言失败:响应从findQuery必须是一个数组,而不是未定义

我检查了每一个SO答案(如这一个这一个)我可能会发现类似的错误,像我的,所提供的解决方案没有帮助我解决我的问题。



我正在学习Ember,并试图与Ember Data RESTAdapter和RESTSerializer混淆,以修复 OMDB API



我收到的错误的最多答案建议使用格式错误的JSON (例如未名称的属性名称),但我确定,在 MovieSerializer.extractArray 中序列化JSON时,我确实没有错误。



我尝试添加 normalize 方法以及 normalizeHash ,但是如上所述,我找不到属性无效或缺失( id 在那里)需要被归一化。



JSON已收到:

  {
标题:纸浆小说,
年 :1994,
评分:R,
发布:1994年10月14日,
运行时间:154分钟,
:犯罪,戏剧,惊悚片,
导演:Q uentin Tarantino,
作家:Quentin Tarantino(故事),Roger Avary(故事),Quentin Tarantino,
演员:Tim Roth,Amanda Plummer,Laura Lovelace,John Travolta ,
剧情:两个暴徒打死的男人,一个拳击手,一个流氓的妻子和一对歹徒的歹徒的生活交织在四个暴力和救赎的故事中。
语言: 英语,西班牙语,法语,
国家:美国,
奖:赢得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,
Type:movie,
响应:True
}

标记的代码部分在下面)

  {
电影:[
{
id:1,
title:纸浆小说,
年:1994,
评级:R,
发行:1994年10月14日,
运行时间:154分钟,
流派:犯罪,戏剧,惊悚,
导演:昆汀·塔伦蒂诺,
作家:Quentin Tarantino(故事),Roger Avary(故事),Quentin Tarantino,
演员:Tim Roth,Amanda Plummer,Laura Lovelace,John Travolta,
情节:两个暴徒打死的男人,一个拳击手,流氓的妻子和一对歹徒的歹徒的生活交织在四个暴力和救赎的故事中。
语言:英语,西班牙语,法国,
国家:美国,
奖:赢得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,
type:movie,
response:True
}
]
}

我的应用程序的相关代码是:



模型

  var Movie = DS.Model.extend({
title:DS.attr('string'),
year:DS.attr('string'),
评分:DS.attr('string'),
发布:DS.attr('string'),
运行时:DS.attr('string'),
类型: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'),
海报:DS.attr('string'),
metascore: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({
model:function(){
return this.get('store')。find ',{title:'Pulp Fiction'}); //在RESTAdapter
}
}中调用findQuery;

适配器

  var MovieAdapter = DS.RESTAdapter.extend({

//请求发送到http://www.omdbapi.com/?t=pulp+fiction& ; y =& plot = short& r = json
buildURL:function(item){
var title = item.title.trim()。replace(/ \s + /,'+') .replace(/ [AZ] / g,function(val){
return val.toLowerCase();
});
returnhttp://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)); //这个点上的SERIALIZED JSON已经登录
this._super(store,type,payload);
}
});


解决方案

确定,所以我发现错误源。 p>

我忘了在 extractArray 中返回数组。



我所做的更改只是:

  extractArray:function存储,类型,有效载荷){
// ...
return this._super(store,type,payload); //添加此返回语句
}

还有一件事要注意其他人看这个问题。不要在没有特别需要的情况下覆盖内置的钩子,如 normalize extractArray ,并确保满足必要条件那些钩子(如返回值,调用 super 等)


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

解决方案

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 Assertion Failed:findQuery的响应必须是Array,而不是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆