烬断言失败:从findQuery的反应必须是一个数组,而不是不确定的 [英] Ember Assertion Failed: The response from a findQuery must be an Array, not undefined

查看:158
本文介绍了烬断言失败:从findQuery的反应必须是一个数组,而不是不确定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 错误在处理路径:指数断言失败:从findQuery的反应必须是一个数组,而不是不确定的

我检查每一个SO回答(像<一个href=\"http://stackoverflow.com/questions/22560929/ember-js-error-assertion-failed-the-response-from-a-findall-must-be-an-array\">this 之一或<一个href=\"http://stackoverflow.com/questions/24291477/ember-data-assertion-failed-error-assertion-failed-the-response-from-a-finda\">this 之一),我可以像我类似的错误发现并没有提供解决方案的帮助我解决我的问题。

我学习余烬和周围试图惹灰烬数据RESTAdapter和RESTSerializer修复从 OMDB JSON响应API

大多数答案我正在提出一个畸形的JSON(如uncamelized属性名称)的错误,但我pretty肯定没有什么错我如何序列化JSON在 MovieSerializer.extractArray

我曾尝试添加正常化方法,以及 normalizeHash 但是,如前所述,我无法找到一个属性这是无效的或丢失( ID 有)需要进行标准化。

JSON收到:

  {
    标题:低俗小说,
    年:1994年,
    额定:R,
    发布:1994年10月14日
    运行时:154分钟,
    类型:犯罪,剧情,惊悚
    导演:昆汀·塔伦蒂诺
    作家:昆汀·塔伦蒂诺(故事),罗杰Avary(故事),昆汀·塔伦蒂诺
    演员:蒂姆·罗斯,阿曼达·普拉莫,劳拉·洛夫莱斯,约翰·特拉沃尔塔
    暗算:两个暴徒击中人的生活,一个拳击手,一个黑帮老大的妻子和一对食客强盗暴力和赎回四个故事纠结。
    语言:英语,西班牙语,法语
    国:USA,
    大奖:。荣获奥斯卡1另一个63胜&放大器; 47项提名
    海报:http://ia.media-imdb​​.com/images/M/MV5BMjE0ODk2NjczOV5BMl5BanBnXkFtZTYwNDQ0NDg4._V1_SX300.jpg
    Metascore:94,
    imdbRating:8.9,
    imdbVotes:1039031,
    imdbID:tt0110912,
    类型:电影,
    回应:真
}

序列化JSON(登录标记code部分波纹管)

  {
    电影: [
        {
            标识:1,
            头衔:低俗小说,
            年:1994年,
            评为:R,
            释放:1994年10月14日
            运行:154分钟,
            流派:犯罪,剧情,惊悚
            导演:昆汀·塔伦蒂诺
            作家:昆汀·塔伦蒂诺(故事),罗杰Avary(故事),昆汀·塔伦蒂诺
            演员:蒂姆·罗斯,阿曼达·普拉莫,劳拉·洛夫莱斯,约翰·特拉沃尔塔
            暗算:两个暴徒击中人的生活,一个拳击手,一个黑帮老大的妻子和一对食客强盗暴力和赎回四个故事纠结。
            语言:英语,西班牙语,法语
            国:USA,
            大奖:。荣获奥斯卡1另一个63胜&放大器; 47项提名
            大字报:http://ia.media-imdb​​.com/images/M/MV5BMjE0ODk2NjczOV5BMl5BanBnXkFtZTYwNDQ0NDg4._V1_SX300.jpg
            metascore:94,
            imdbRating:8.9,
            imdbVotes:1039031,
            imdbId:tt0110912,
            类型:电影,
            回应:真
        }
    ]
}

我的应用程序的相关code是:

型号

  VAR电影= DS.Model.extend({
    标题:DS.attr(字符串),
    同期:DS.attr(字符串),
    额定:DS.attr(字符串),
    发布:DS.attr(字符串),
    运行时间:DS.attr(字符串),
    流派:DS.attr(字符串),
    导演:DS.attr(字符串),
    作者:DS.attr(字符串),
    演员:DS.attr(字符串),
    剧情:DS.attr(字符串),
    语言:DS.attr(字符串),
    国家:DS.attr(字符串),
    奖项:DS.attr(字符串),
    海报:DS.attr(字符串),
    metascore:DS.attr(字符串),
    imdbRating:DS.attr(字符串),
    imdbVotes:DS.attr(字符串),
    imdbId:DS.attr(字符串),
    类型:DS.attr(字符串),
    回应:DS.attr(字符串)
});

索引路线

  VAR IndexRoute = Ember.Route.extend({
    型号:功能(){
        。返回this.get('店')找到('电影',{标题:低俗小说}); //在RESTAdapter调用findQuery
    }
});

适配器

  VAR MovieAdapter = DS.RESTAdapter.extend({    //请求发送到http://www.omdbapi.com/?t=pulp+fiction&y=&plot=short&r=json
    buildURL:函数(项目){
        VAR标题= item.title.trim()取代(/ \\ s + /,'+')。取代(/ [A-Z] /克,功能(VAL){
            返回val.toLowerCase();
        });
        回归http://www.omdbapi.com/?t=+标题+&安培; Y =&安培;情节=短和放大器; R = json的;
    }    findQuery:功能(存储,类型,查询){
        返回this.ajax(this.buildURL(查询),'GET');
    }});

串行

  VAR MovieSerializer = DS.RESTSerializer.extend({    extractArray:功能(存储,类型,负载){
        变种电影= [{
            ID:1 //硬code现在一个id
        }];        VAR camelKey;
        对于(有效载荷VAR键){
            camelKey = Ember.String.decamelize(密钥).camelize();
            动画[0] [camelKey] =负载[关键]
        }        有效载荷= {电影:电影};
        的console.log(JSON.stringify(有效负载)); //序列化的JSON以上被记录在这个点
        this._super(存储,类型,负载);
    }
});


解决方案

好吧,我发现错误源。

我忘了返回 extractArray 的数组。

我所做的更改很简单:

  extractArray:功能(存储,类型,负载){
    // ...
    返回this._super(存储,类型,负载); //添加这个return语句
}

还有一点要注意,对于其他人在看这个问题。不要覆盖内置挂钩,比如正常化 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 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.)

这篇关于烬断言失败:从findQuery的反应必须是一个数组,而不是不确定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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