Swati
Swati

Reputation: 852

findRecord giving error "Cannot read property '_internalModel' of undefined"

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.

Upvotes: 7

Views: 3108

Answers (4)

haley y
haley y

Reputation: 21

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()

Upvotes: 2

jos
jos

Reputation: 1070

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 
}

Upvotes: 2

Pedro
Pedro

Reputation: 3781

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 });
});

Upvotes: 1

Swati
Swati

Reputation: 852

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 :)

Upvotes: 0

Related Questions