0
votes

I get "application.js:72 TypeError: _this2.get(...).toArray is not a function" error in the following code. I am trying to push what comes from the server response to the model so that model is dynamically updated. This is happening in controller. Controller intercepts an action from another component in the template. What is going wrong here?

actions: {
sendData: function () {
  this.set('showLoading', true);

  let data = {
    startTime: date.normalizeTimestamp(this.get('startTimestamp')),
    endTime: date.normalizeTimestamp(this.get('endTimestamp')),
    type: constants.ENTERPRISE.REPORTING_PAYMENT_TYPE
  };

  api.ajaxPost(`${api.buildV3EnterpriseUrl('reports')}`, data).then(response => {
    this.set('showLoading', false);

    console.log("Comes here!!!!");
    console.log(this.get('model'));
    this.get('model').toArray().addObject(Ember.Object.create(response));

    return response.report;
  }).catch(error => {
    this.set('showLoading', false);
    if (error.status === constants.HTTP_STATUS.GATEWAY_TIMEOUT) {
      this.notify.error(this.translate('reports.report_timedout'), this.translate('reports.report_timedout_desc'));
    } else {
      this.send('error', error);
    }
  });
}
2

2 Answers

0
votes

Is your 'model' an ember data model? If so, it might actually be a promise, and toArray will not work in that case. To overcome this issue, either return the model once it has resolved fully or use .then to convert it to array. As a side note, if your only goal is to add the response to your model, you could do without all the conversions, and just add the object to your model directly. Probably something like "Ember.set(model, 'newAttr', Ember.Object.create(response)); and use the info stored in newAttr.

Hope this helps! :)

0
votes

If you return RSVP.hash then you can't do this.get('model').toArray() since now model is just object. I guess you might need to take exact properties from model something like this.get('model.reportsOrSomethingWhichYouReturnFromModelHook').