简体   繁体   中英

EmberJS Computed Property, Uncaught TypeError: Cannot read property 'findBy' of undefined

I am working on an Ember CLI project (version 0.0.46)

While trying to use Ember Arrays findBy() inside a computed property, I get following error:

Uncaught TypeError: Cannot read property 'findBy' of undefined

  • I have checked the locations array. It contains 2 objects before the findBy() is used on it.

Template

{{view Ember.Select
     content=locations
     value=locationId
     optionValuePath="content.id"
     optionLabelPath="content.name"
     prompt="- Please select a Location -"
     class="form-control input-md"
     selection=selectedLocation
}}

Computed Property:

selectedLocation:function(){
  var locations = this.get('controllers.xyz.locations');
  var matchedLoc =locations.findBy('id','loc2');
  return matchedLoc;
}.property('controllers.xyz.locations')
  • Note: For some reason, the above logic works fine when I use it inside the actions hook. Also, the locations array is null only when the first call is made to the computed property.

Any help will be appreciated. Thanks!

It is likely that this function is getting called when model.locations is still null, and will be called again later when model.locations is set. Add .property('model.locations') , and do a check for model.locations being null before trying to call findBy .

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