4

I have a lightning component with an array of checkboxes which are loaded dynamically where I select a field on account as a criteria. For test purposes I used Billingcity.

The filter works for this part and returns the selected cities. The second part is where I want to return all the records based on the selected cities.

The error I get from this second part is variable is undefined. In my code is defined the variable so why is it still giving me this error?

Component

<aura:attribute name="selectedCities" type="Account[]" default="[]" description="accounts" />

Javascript

checkboxes
        .filter(checkbox => checkbox.get("v.value"))    // Get only checked boxes
        .forEach(checkbox => selected.push(checkbox.get("v.label")));   // And get the labels
        component.set("v.selectedCities", selected);    // Set to display

        console.log('v.selectedCities:'+  selected);
        console.log('v.component.get("c.getAllAccounts"):'+ component.get("c.getAllAccounts"));

        console.log('filteredAccounts '+ filteredAccounts);
        var filteredAccounts = component.get("c.getAllAccounts").filter(account => selectedCities.indexOf(account.BillingCity) > -1);
        console.log('filteredAccounts '+ filteredAccounts);

Debug log

v.selectedCities:Burlington

v.component.get("c.getAllAccounts"):SecureAction: [object Object]{ key: {"namespace":"c"} }

filteredAccounts undefined
3
  • you are declaring filteredAccounts after having referenced it in your code as far as i can tell
    – glls
    Commented Jan 30, 2018 at 23:20
  • did you reorder your code, and declare your variable before trying to reference it in your console.log ?
    – glls
    Commented Jan 31, 2018 at 1:56
  • @glls Yes I did, same results. I did some research and It also could be because of resulting in an empty array which I do not understand in this case as the selectedCities has a value in the log. So probably it something else
    – Thomas
    Commented Jan 31, 2018 at 2:00

1 Answer 1

1

Problem is here...

console.log('filteredAccounts '+ filteredAccounts);
var filteredAccounts = component.get("c.getAllAccounts").filter(account => selectedCities.indexOf(account.BillingCity) > -1);
console.log('filteredAccounts '+ filteredAccounts);

filteredAccounts declaration gets hoisted, so its not an error, but its undefined on the first console.log()

you're assignment to filteredAccounts will fail, since component.get("c.getAllAccounts") returns an action which does not have a filter method on it. You probably want to execute the action and filter the result? You'll have to use $A.enqueueAction() and a callback to do so.

The second filterAccounts doesn't run, since you have an error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .