7

In our ember app, we are using following versions of ember-data and ember-data-factory-guy.

package.json

"ember-cli": "^1.13.8",
"ember-data": "1.13.9",
"ember-data-factory-guy": "1.13.10",

Note: we are using active-model adapter, not yet migrated to the json-api adapter.

import ActiveModelAdapter from 'active-model-adapter';
export default ActiveModelAdapter.extend({

Route: item.js

export default Ember.Route.extend(({
  model(params) {
    return this.store.findRecord('item', params.item_id);
  }
});

Its working fine in development mode, but while running test cases, am facing following issue:

Test Case for "display single item" fails with following error:

{
  "message": "Cannot read property '_internalModel' of undefined",
  "name": "TypeError"
}

ember-data/lib/system/stpre/finder.js, fails at return statement

return promise.then(function (adapterPayload) { Ember.assert("You made a request for a " + typeClass.typeClassKey + " with id " + id + ", but the adapter's response did not have any data", adapterPayload);

return store._adapterRun(function () {
  var requestType = get(serializer, 'isNewSerializerAPI') ? 'findRecord' : 'find';
  var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, id, requestType);
  //TODO Optimize
  var record = pushPayload(store, payload);
  return record._internalModel;
});

(https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/store/finders.js#L32)

Are we missing anything here? Can anyone please help me to resolve this? I have tried by upgrading versions to latest, but still facing same issue.

Swati
  • 842
  • 10
  • 26
  • 1
    Are you stubbing the response? `this.store.findRecord('item', params.item_id)` issues a request to the server. – vikram7 Oct 01 '15 at 14:56
  • Yeah, it is sending request to server, but I have that data available in my ember-data store, so was expecting to pick it from there. Hence not stubbed that response. – Swati Oct 02 '15 at 09:27
  • `this.store.findRecord` will always send a request even though you have it available in your store. You should try `peekRecord` if you don't want to send a request. – vikram7 Oct 02 '15 at 15:07
  • Thanks for the response @vikram7 Finally i was able to solve it.. I would add it as answer. Re: your above comment: refer this http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_new-adapter-hooks-for-better-caching – Swati Oct 02 '15 at 16:22

4 Answers4

2

In my case the problem was that the server's response didn't have the root element.

Server was returning for a user:

{
  surname: 'surname',
  name: 'name',
  _id: 56ead1ace85b04be4a7e50e6 
}

instead:

user: {
  surname: 'surname',
  name: 'name',
  _id: 56ead1ace85b04be4a7e50e6 
}
jos
  • 1,070
  • 12
  • 22
2

If you're querying the server using findRecord(), Ember expects the response to be in the form

{singularModelName: {...}}

If you're querying the server using query(), Ember expects the response to be in the form

{pluralModelName: [...]}

The type error will occur if you're not following that response pattern while using findRecord()

haley y
  • 21
  • 1
1

I mostly post this here as a reminder for myself. I run into this issue every couple weeks and come here to find an answer :)

This error was thrown while I was running acceptance tests because I forgot to tell ember-cli-mirage to generate fake models:

beforeEach(function() {
  server.create('user', { id: window.sessionUser.id });
  server.create('project', { userId: window.sessionUser.id });
});
Pedro
  • 3,511
  • 2
  • 26
  • 31
0

Finally got the exact cause:

In my adapter/application.js

// Ember Data 2.0 Reload behavior
shouldReloadRecord: function() { return true; },
shouldReloadAll: function() { return true; },
shouldBackgroundReloadRecord: function() { return true; },
shouldBackgroundReloadAll: function() { return true; },

These lines I had added while fixing deprecation warnings, and because of this, it was causing records to be loaded always, although they were present in ember-data store. So now I just removed those.

http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_new-adapter-hooks-for-better-caching This reference helped me to get it understand much better :)

Swati
  • 842
  • 10
  • 26