Cannot read property 'determineRelationshipType' when trying to save() Model with hasMany Relationship

三世轮回 提交于 2020-02-03 10:41:26

问题


I'm running into a tough bug when trying to save a record with the LocalStorage Adapter that has a hasMany relationship (Using Ember CLI). What I'm trying to do is save a product to a bag when a user clicks on a "Add to Bag" button. I'm getting this error in my console:

Uncaught TypeError: Cannot read property 'determineRelationshipType' of undefined

Product Model:

import DS from 'ember-data';

export default DS.Model.extend({
  ...
  bag: DS.belongsTo('bag')

});

Bag Model:

import DS from 'ember-data';

export default DS.Model.extend({
  products: DS.hasMany('product', {async: true})
});

Here's the action in the controller:

import Ember from "ember";

export default Ember.ArrayController.extend({
  actions: {
    addToBag: function(model) {
      var bag = this.store.createRecord('bag');
      bag.get('products').then(function(products) {
        products.pushObject(model);
        bag.save();
      });
    }
  }
});

Would anyone have an idea as to what's going wrong? Or another way to approach this? Seems like a similar issue was reported here. Would greatly appreciate any help! Thank you in advance.


回答1:


I started a project using the emberfire adapter and ran into the same issue.

Without going to deep into this, it looks like ember-data beta.10 deprecated a feature that was necessary for hasMany to work. (Further reading https://github.com/firebase/emberfire/issues/123)

Downgrading to ember-data beta.8 fixed the issue for me.

This is necessary until the adapters (emberfire and/or localstorage) can be updated.

In my ember-cli project I did:

rm -rf vendor/ember-data/ bower cache clean ember-data Edit files vendor/emberfire/bower.json and vendor/emberfire/.bower.json to say "ember-data": "1.0.0-beta.8".

bower install




回答2:


I re-investigated this issue and looks like it was addressed in a recent update to ember-localstorage-adapter. Specifically, the reference to DS.RelationshipChange was removed.

In my bower.json, I defined my ember-data version back to 1.0.0-beta.11 and also defined my ember-localstorage-adapter version to the latest version, 0.5.0. Here's the relevant info in the bower.json file:

{
  "name": "****",
  "dependencies": {    
    "ember": "1.8.1",
    "ember-data": "1.0.0-beta.11",
    "ember-localstorage-adapter": "~0.5.0",
  }
}

This error is no longer appearing!



来源:https://stackoverflow.com/questions/26309726/cannot-read-property-determinerelationshiptype-when-trying-to-save-model-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!