For some reason, the push in the code block below is not updating the array. The console.log statement displays the array with 0 elements. I created a local array variable and pushed to it with no issues, so it seems that problem is pushing to a tracked variable in the LWC. Any thoughts?
import { LightningElement, api, track } from 'lwc';
export default class TPM_MS_MaterialSubcontractors extends LightningElement {
@api matsubs;
@api display;
@track countries=[];
@track addresses = [];
constructor() {
super();
this.getCountries();
this.getAddresses();
}
getAddresses() {
let addressList = [
{id: '1', address1: '123 Main Street', address2: 'Suite 100', city: 'Richmond', region: 'Virginia', postalcode: '23835', countrycode: '1'}
];
addressList.forEach(element => {
let addressString = element.address1 + (element.address2 === '' ? '' : ", " + element.address2) + ', ' + element.city;
this.addresses.push({id: element.id, address: addressString });
});
console.log(this.addresses);
}
getCountries() {
this.countries = [{value: '1', label: 'United States'}, {value: '2', label: 'United Kingdom'}, {value: '3', label: 'India'}];
}
}
Best Answer
This problem was resolved by removing the @track annotation.