0

I am getting below error: when I am trying to use push (function) to add array in another array.

enter image description here

Javascript code :


export default class CarDisplay extends LightningElement {
   @track carvalue; 
    @wire(listCard)
    cartypewire({data,error}){
        if(data){
            this.carvalue={value:'',label:'Select Car'};
            console.log('data',data);
            data.forEach(element => {
            const cartype={};
            cartype.label =element.Name;
            cartype.value =element.Id;
            console.log('element',element);
            console.log('element.name',element.Name);
            console.log('element.id',element.Id);
            console.log('cartype',cartype);
          this.carvalue.push(cartype);//getting error  
            console.log('carvalue',this.carvalue);
           });
        }else if(error){  console.log('error',error);
    this.showToast('ERROR',error.body.message,'error');}
    }
    }
1

You need to change this this.carvalue={value:'',label:'Select Car'}; to this.carvalue=[{value:'',label:'Select Car'}];

In javascript push is available only in array. When you define {} it becomes an object.

0
this.carvalue={value:'',label:'Select'}

carvalue is an object not an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.