最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - findRecord giving error "Cannot read property '_internalModel' of undefined" - St

programmeradmin1浏览0评论

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

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

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./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./danielspaniel/ember-data-factory-guy/issues/136
Share Improve this question asked Oct 1, 2015 at 14:20 SwatiSwati 86210 silver badges28 bronze badges 4
  • 1 Are you stubbing the response? this.store.findRecord('item', params.item_id) issues a request to the server. – vikram7 Commented Oct 1, 2015 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 Commented Oct 2, 2015 at 9: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 Commented Oct 2, 2015 at 15:07
  • Thanks for the response @vikram7 Finally i was able to solve it.. I would add it as answer. Re: your above ment: refer this emberjs./blog/2015/06/18/… – Swati Commented Oct 2, 2015 at 16:22
Add a ment  | 

4 Answers 4

Reset to default 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 
}

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

I mostly post this here as a reminder for myself. I run into this issue every couple weeks and e 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 });
});

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

发布评论

评论列表(0)

  1. 暂无评论