Error evaluating App.Product.findQuery

ε祈祈猫儿з 提交于 2020-03-05 07:59:10

问题


I'm trying to get data from an API like this:

    App.Store = DS.Store.extend({
        revision: 12,
        adapter: DS.RESTAdapter.create({
            host: 'http://api.my-api/v1/products(name=my-name)'
        })
    });

    App.Product = DS.Model.extend({
        name: DS.attr('string')
    });

    App.ApplicationRoute = Ember.Route.extend({
        model: function () {
            return App.Product.findQuery({show: 'sku,name', format: 'json', apiKey: 'MyApIkEy123'});
        }
    });

The error I get in the console is:

Error while processing route: index undefined is not a function (evaluating 'App.Product.findQuery({show: 'sku,name', format: 'json', apiKey: 'MyApIkEy123'})')

The JSON should look like this:

    {
        "from": 1,
        "to": 10,
        "total": 10,
        "products": [
            {
                "sku": 1234567,
                "name": "Great Product"
            }
    }

回答1:


They are several problems on your post.

The first one is that you do not run App.Product.findQuery in your route but a this.store.find(yoursamequery) as App.Product extends DS.Model and DS.Model dosen't have findQuery method (thus you get undefined is not a function :)) http://emberjs.com/api/data/classes/DS.Model.html

I think that your "format" and "apiKey" are not data filter but request parameters which have to be passed to your backend api right ? If so you should create an applicationAdapter with those parameters defined as in the documentation example :

http://emberjs.com/api/data/classes/DS.RESTAdapter.html




回答2:


In the model hook, try using:

return this.store.findQuery('product', {show: 'sku,name', format: 'json', apiKey: 'MyApIkEy123'});




回答3:


It looks like you are trying to get your API to provide attributes that aren't in your model (i.e. sku, salePrice). Is that right? What does the response to that API call look like? If Ember Data is trying to set those attributes in your model object and not finding them, this could be the issue.



来源:https://stackoverflow.com/questions/27611053/error-evaluating-app-product-findquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!