1

I've got an odd issue with ember js, when i push an object to a model. Here is my code:

// Environment.js
  EXTEND_PROTOTYPES: {
    // Prevent Ember Data from overriding Date.parse.
    Date: true,
    Array: true,
  }

Route.js

 model () {
  return Ember.RSVP.hash({
    newCollection: this.get('store').createRecord('collection'),
    book1: this.get('store').createRecord('book'),
    book2: this.get('store').createRecord('book')
  })
}

Controller

actions:{
  addCollection(model) {
    model.newCollection.pushObject(model.book1);
    model.newCollection.pushObject(model.book2);
  },
}

Now I'm not sure what the issue, but I'm trying to push the book model into the collection, however, I get an issue with this as the console log suggests that pushObject is not a function. I've updated my Environment.js as other questions have suggested, however this still is an issue.

Collection Model

// collection Model
export default DS.Model.extend({
    name: DS.attr('string'),
    city: DS.attr('string'),
    books: DS.hasMany('book', { async: true })
});

The book model

//book Model
export default DS.Model.extend({
    title: DS.attr('string'),
    description: DS.attr('string'),
    collection: DS.belongsTo('collection', {async: true})
});
3
  • please show your models Commented Sep 23, 2017 at 15:24
  • @Lux the models have been added to the question Commented Sep 23, 2017 at 17:03
  • duplicate Ember : addObject/ pushObject is not a function Commented Apr 15, 2022 at 13:20

2 Answers 2

2

You have a typo. You're pushing to the actual model instead of the books hasMany relationship:

actions: {
  addCollection(model) {
    model.newCollection.get('books').pushObject(model.book1);
    model.newCollection.get('books').pushObject(model.book2);
  },
}
Sign up to request clarification or add additional context in comments.

2 Comments

Actually no. model is not an ember Object. He's returning return Ember.RSVP.hash({...}).
Addressed the comment.
1

Your problem is, that newCollection is a collection record. So you should do:

model.newCollection.get('books').pushObject(model.book1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.