问题
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