genkgo / ember-localforage-adapter

Offline usage for Ember Data, based on localstorage adapter, but now uses Mozilla's localforage as data source
Other
133 stars 26 forks source link

findRecord not retrieving existing data #64

Closed gilbcharlene closed 7 years ago

gilbcharlene commented 8 years ago

Ember 2.7.1 Ember-data 2.7.0 ember-localforage-adapter 2.2.0

When I query the store for existing records, nothing is found. I get the following error:

"Assertion Failed: normalizeResponse must return a valid JSON API document:
    * One or more of the following keys must be present: "data", "errors", "meta"."

I have followed the documentation and set up the application adapter like this:

import LFAdapter from 'ember-localforage-adapter/adapters/localforage';

export default LFAdapter.extend({
    namespace: 'myapp'
});

` and my model like this import DS from 'ember-data';

export default DS.Model.extend({
  email: DS.attr('string'),
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  lists: DS.hasMany('list'),
  items: DS.hasMany('item')
});

And creating user records appears to be successful, as seen in this image image

However when I try to find a record by email, I receive the error message I posted above.

self
            .get('store')
            .query(
              'user',
              {
                filter: {
                  email: emailAddress
                }
              }
            )
            .then(
              function(existingUser){
                console.info( '---- An existing user was found! ----' );
                console.debug(existingUser);
                // login with existing user
                self.session.login(existingUser);
                // go to collections
                self.transitionTo('app.welcome');
              }
            )
            .catch(
              function(error){
                console.info('---- No user was found! Creating new user: ' + emailAddress + ' ----');
                console.debug(error); });
frederikbosch commented 8 years ago

Maybe incompatibility with newer Ember / Ember Data versions?

iamolivinius commented 8 years ago

I find this issue rather confusing :confused:

The title indicates that you have a problem with ember-data's findRecord method but in your last code example you're using the query method. Furthermore your screenshot of the browser storage explorer indicates that you're using a different serializer (looks like the JSONAPISerializer).

Nevertheless, if you try to use query to get a subset of all records from a specific model and you're looking at the current ember guides you might run into a "bug".

Ember Data guides since 2.0 promote the usage of query as follows:

store.query('modelName', {
  filter: {
    key: "value"
  }
})

before Ember Data 2.0 it was:

store.query('modelName', { key: "value" })

ember-localforage-adapter assumes a plain query object like it was in the pre 2.0 guides and like it is documented in the API docs. http://emberjs.com/api/data/classes/DS.Store.html#method_query

frederikbosch commented 7 years ago

Since @iamolivinius has valid points I will close this issue.