Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Web-development
  3. Question
Web-development

pushing to @track array variable says cannot read property push of undefined

Asked by Katherine Dwivedi Jul 29, 2021 1.2K views 1 answer
Share

About this question

This may be a super simple solution, I had this below code copied from another location from the same project, which works really well. Brought it in to another section, changed the variable names, for some reason it doesn't work. I get the error "Cannot read property 'push' of undefined". Also the console.log below get the correct length.

@track cities = []; markerClick(e) { let province = ''; getCitiesByProvince({ province: province, }).then(result => { console.log(result.length) for (let i = 0; i < result>

function run when clicked on a map marker.

export default class MduPenetration extends LightningElement { @track provinces = []; @track cities = []; @wire(onLoadProvinces) provStats({ data }) { if (data) { for (let i = 0; i < data xss=removed> { const el = this.template.querySelector(".map-root"); let mymap = L.map(el).setView([54.139503, -96.653471], 5); L.tileLayer('https://api.mapbox.com/styles/v1/sumchans', { tileSize: 512, zoomOffset: -1 }).addTo(mymap); let bcPopup = L.circle([this.provinces[0].latitude, this.provinces[0].longitude], { id: 'BC', color: 'white', fillColor: '#38c', fillOpacity: 0.5, radius: (this.provinces[0].penetration) * 2500 }).addTo(mymap).on('click', this.markerClick); }); } markerClick(e) { getCitiesByProvince({ province: 'BC' }).then(result => { if (result) { // console.log(this.cities); This says undefined for (let i = 0; i < result>

 What is reason of getting typeerror: cannot read property 'push' of undefined?

Your answer

1 Answer

More Web-development discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest from the JanBask Blog

Guides, tips and career advice from JanBask experts.