Vuejs "Typeerror: Devices.push is not a Function"

Asked

Viewed 230 times

0

I’m trying to push into an Array, but it’s giving this error: "Typeerror: Devices.push is not a Function"

let device = this.$store.getters['bluetooth/Device'].UUID

let devices = firebase.getValue('/connected_devices')
let found = false
if(devices == null)
devices = []

for(let i = 0; i < devices; i++){
   if(device.mac == devices[i].mac){
      device = devices[i]
      found = true
   }
}

if(!found)
this.devices[i].push(device)

device.pi = 1234
device.teste = 4321
firebase.setValue('/connected_devices', devices)
  • What does your Vices return ? it is falling into == null condition?

  • 1

    Here this.devices you reference the Vue instance. If you want to use the method variable remove the this

2 answers

1

I read the code, and I realized you’re trying to access the variable i outside the scope of the for, in addition to trying to access this.devices.

These two variables are causing your typeError

Try the following

if(!found)
    devices.push(device)

1

Your problem is that you are referenced to the instance of Vue and not the variable devices that is inside your method. If you want to reference the variable Devices inside the method remove the this

If you really want to reference a variable in the instance attaches to the date method the property.

data () {
 return {
   devices : []
 }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.