简体   繁体   中英

Cannot convert object to primitive value at Array.toString (native) when populating ember-power-select with array of objects generated dynamically

I need to populate a power-select with objects from the store, but I can only get the options displayed with the hard coded array myOptions . I tried to generate myOption2 dynamically, and apparently got the same output as myOptions , but it throws an error:

ember.debug.js:19845 Uncaught TypeError: Cannot convert object to primitive value
at Array.toString (native)
at Object.addListener (http://localhost:4200/assets/vendor.js:30682:88)
at Object.addObserver (http://localhost:4200/assets/vendor.js:34026:23)
at Array.addObserver (http://localhost:4200/assets/vendor.js:47679:27)
at Class.updateOptions (http://localhost:4200/assets/vendor.js:88968:17)
at invoke (http://localhost:4200/assets/vendor.js:11825:16)
at Queue.flush (http://localhost:4200/assets/vendor.js:11891:11)
at DeferredActionQueues.flush (http://localhost:4200/assets/vendor.js:11699:17)
at Backburner.end (http://localhost:4200/assets/vendor.js:11013:25)
at Backburner.run (http://localhost:4200/assets/vendor.js:11135:18)

function addListener(obj, eventName, target, method, once) {
_emberMetalDebug.assert('You must pass at least an object and event name to Ember.addListener', !!obj && !!eventName);
_emberMetalDebug.deprecate('didInitAttrs called in ' + (obj && obj.toString && obj.toString()) + '.', eventName !== 'didInitAttrs', {
  id: 'ember-views.did-init-attrs',
  until: '3.0.0',
  url: 'http://emberjs.com/deprecations/v2.x#toc_ember-component-didinitattrs'
});

//template.hbs

{{#power-select options=myOptions onchange=(action "foo") as |opt|}}
  {{opt.name}}
{{/power-select}}

//component.js

myOptions: [
  { name: 'First Option' },
  { name: 'Second Option' }
],

myOptions2: [],

didReceiveAttrs() {
  this.get('model.content').forEach((option) => {
    this.get('myOptions2').push(option._data);
  });
}


//#=> myOptions: [{name: 'First Option'},{"name":"Second Option"}]
//#=> myOptions2 [{name: 'First Option'},{"name":"Second Option"}]

Thanks in advance!

Instead of push you need to use pushObject , only then observer,computed property will fire and templates will be updated automatically.

this.get('myOptions2').pushObject(option._data)

Being said that, I am not sure this will fix your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM