0
votes

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!

1
The error message indicates clearly that locations is undefined. When did you "check the locations array"? Put a console.log after the assignment to locations. BTW, in what context are you defining selectedLocation? It may be that this function is getting called when model.locations is still null, and will be called again later when model.locations is set. Try adding .property('model.locations'), and doing a check for model.locations being null before trying to call findBy. - user663031
The selectedLocation property is called by Ember.Select in the view. i just realised that, the the locations array is null for the first call. If selectedLocation is called for second time, the locations array is filled with objects. Any suggestions why? - Priyank Kapasi
@torazaburo: The undefined error has been solved by checking if the locations array is null or not. Thanks! - Priyank Kapasi

1 Answers

0
votes

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.