Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sting's avatar
Level 1

TypeError: Cannot read properties of null (reading 'includes')

I have this error: TypeError: Cannot read properties of null (reading 'includes')

It shows when clicking on this div:

<div v-for="(drone, index) in drones" class="col-md-4 drone-item mb-25 cursor-pointer" @click="setSelectedModel('drone', drone)" :class="{'card-selected': selected_drones.includes(drone.id)}">

JS function:

setSelectedModel(model_type, model) {
                    if(model_type == 'drone') {
                        if(!this.selected_drones.includes(model.id)) {
                            this.selected_drones.push(model.id);
                            console.log(this.selected_drones)
                            this.selected_drones_model.push(model.model_id);
                        }
}
}

the console.log prints the values being added to the array, but in the div it show null

0 likes
1 reply
lbecket's avatar

Are you properly initializing selected_drones?

You could avoid the error by adding a null coalescing operator to your function in the form of a ? before includes:

setSelectedModel(model_type, model) {
  if (model_type === 'drone') {
    if (!this.selected_drones?.includes(model.id)) {
      this.selected_drones.push(model.id);
      console.log(this.selected_drones);
      this.selected_drones_model.push(model.model_id);
    }
  }
}

That doesn't necessarily address the underlying cause, but it would eliminate the error.

Please or to participate in this conversation.