Reputation: 661
I am using the latest Ember Data version 1.0.0-beta. I am trying to add objects to a hasMany relationship in local storage but am having some problems.
I have two models:
App.Item = DS.Model.extend({
description: DS.attr('string'),
});
App.Basket = DS.Model.extend({
selectedItems: DS.hasMany('item'),
name: DS.attr('string'),
})
I have an array called "Items_descriptions" that contains an array of description strings. I am trying to insert items into a basket using the following manner:
var basket_record = App.Basket.store.createRecord('basket', { name: 'Test_record' });
var itemsarray = [];
Items_descriptions.forEach(function(description){
var item = App.Item.store.filter('item', function(record){return record.get('description') == description}).objectAt(0);
itemsarray.push(item);
});
basket_record.get('selectedItems').pushObjects(itemsarray); // ***errors out here****
basket_record.save();
However I get the following error at this line basket_record.get('selectedItems').pushObjects(itemsarray);" :
Uncaught TypeError: Cannot read property 'typeKey' of undefined
I also tried using the App.Model.store.find method, however that also doesnt work:
var basket_record = App.Basket.store.createRecord('basket', { name: 'Test_record' });
var itemsarray = [];
Items_descriptions.forEach(function(description){
App.Item.store.find('item', {'description': description}).then(function(item){
itemsarray.push(item);
});
});
basket_record.get('selectedItems').pushObjects(itemsarray);
basket_record.save();
The App.Mode.store.find method works but it doesnot add the data successfully into the selectedItems attribute. When I check localstorage, the name attribute in basket is correctly filled out, but the array for selected Items is empty.
From what I understand, App.Model.store.find returns a promise and an App.Model.store.filter().objectAt(0) returns the object itself.
Any idea what I am doing wrong here? Any help is greatly appreciated. Thanks so much!
Upvotes: 1
Views: 565
Reputation: 661
The way I resolved this was by changing my ember data to the online version. I was using a downloaded version of ember data (downloaded on 13th September). But in the past ten days, there have been several edits made to the ember-data js library. I am guessing the error was initially experienced due to bugs in the previous versions. After I updated to the latest online version of ember data, the hasMany relationship is getting updated now.
FYI: I am using the first method of App.Model.store.filter(..).objectAt(0) to get the objects and set it to the hasMany relationship.
Upvotes: 1