Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • posted in issues of ember-data-factory-guy https://github.com/danielspaniel/ember-data-factory-guy/issues/136
like image 524
Swati Avatar asked Oct 01 '15 14:10

Swati


3 Answers

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 
}
like image 181
jos Avatar answered Nov 14 '22 22:11

jos


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

like image 32
haley y Avatar answered Nov 14 '22 20:11

haley y


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 });
});
like image 1
Pedro Avatar answered Nov 14 '22 21:11

Pedro