kck
kck

Reputation: 609

Ember Data 1.13 findAll isn't working

I'm just getting started with Ember 2.0 and I'm trying to build a simple CRUD application using Ember Data. I've been following the Ember guides online but I've run into an issue.

I'm populating default data into my store using this.store.push in my routes/application.js file. When I use this.store.findRecord(modelName, id) immediately after, it seems as though my data is persisting successfully:

enter image description here

However, when I use this.store.findAll(modelName), I get a 404 as a result of an "Adapter operation failed" error:

enter image description here

I've done some searching around, but given such an ambiguous error I haven't had much luck. If anyone has any insight I'd greatly appreciate it! Here's the code I used to create the console output:

var wine = this.store.findRecord('wine', 5);
var wines = this.store.findAll('wine');
console.log(wine);
console.log(wines);

And an example of a model that I'm creating:

this.store.push('wine', {
  id: '5',
  name: 'Pink Moscato',
  vintage: '2014',
  description: 'The wine has aromas of Mandarin orange and sweet ' +
    'jasmine. It has subtle flavors of cherry, raspberry, and ' +
    'pomegranate.',
  imageUrl: 'https://placehold.it/290x290'
});

Upvotes: 0

Views: 460

Answers (1)

Daniel
Daniel

Reputation: 18672

this.store.findAll(modelName) will fire HTTP request to your API backend. From what I've read it seems like you have no API server, and you only use store.push to mock data in Ember Data. So, I would recommend you to use:

this.store.peekAll(modelName) which will not fire any request and will give you local data.

I find it odd that findRecord works, while findAll doesn't.

findRecord works for you while findAll doesn't because findRecord attemps to get local version of record, which is already present in your case (you use store.push) and tries to reload record in background to get latest data (you should see in your dev tools that even with findRecord request is fired to refresh it's data).

findAll is also cached, but it fires request first time when it is called.

I recommend you to see my previous answer for more info and details of peekAll and findAll.

Also, what would be an example of an API server in this case?

Depends whether you want to use RESTAdapter or JSONAPIAdapter. In case of RESTAdapter your example response from API server (to findAll('wine')) could be:

{
    "wines": [
        {
            "id": 5,
            "name": "Pink Moscato",
            "vintage": "2014",
            "description": "text",
            "imageUrl": "https://placehold.it/290x290"
        }
    ]
}

I was under the impression that Ember Data could be used by itself.

Yeah, it could be used by itself, but you need to tweak your Adapter.

Upvotes: 1

Related Questions