Search code examples
javascriptlocal-storageionic4

Ionic variable loss


I'm writing an app that allow my RPI to connect to the internet via bluetooth This part works, but i'm trying to store the ID of my RPI on the phone, The problem is that my variable loses their value (null)

Here's the code

              timeout(10000).then( () => {
              this.ble.read(
                this.deviceID,
                this.uuidConfig.serviceUUID,
                this.uuidConfig.readStatusUUID,
              ).then( (data) => {
                  const status = JSON.parse(new TextDecoder().decode(data));
                  if (this.ble.isConnected) {
                    if(status.ID !== undefined){
                      this.presentToast("Success !");
                      this.storage.get('SpectR').then(spectrID => {
                     // At this exact location, status.ID is null, I don't get whyy
                        if (spectrID === null){
                          let arrId = status.ID.split()
                          this.storage.set('SpectR', arrId ).then(r => console.log('done') )
                        }
                        else{
                          spectrID.push(status.ID)
                          this.storage.set('SpectR', spectrID ).then(r => console.log('done') )
                        }
                      })
                      this.navCtrl.back()
                    } else {
                      this.presentToast("Wrong password :/")
                    }
                  } else if (!this.ble.isConnected) {
                    this.presentToast("Something went wrong :/")
                  }
                }
              )
            }
          )

Do you have any idea? Before adding the storage part, the value never got overwritten


Solution

  • Try Debug, I can't comment on questions yet... Respond with the value of status

    timeout(10000).then(() => {
        this.ble.read(
            this.deviceID,
            this.uuidConfig.serviceUUID,
            this.uuidConfig.readStatusUUID,
        ).then((data) => {
            const status = JSON.parse(new TextDecoder().decode(data));
            // DEBUG START
            // DEBUG START
            // DEBUG START
            console.info('DEBUG START')
            console.log({ status })
            console.info('DEBUG END')
            // DEBUG END
            // DEBUG END
            // DEBUG END
            if (this.ble.isConnected) {
                if (status.ID !== undefined) {
                    this.presentToast("Success !");
                    this.storage.get('SpectR').then(spectrID => {
                        // At this exact location, status.ID is null, I don't get whyy
                        if (spectrID === null) {
                            let arrId = status.ID.split()
                            this.storage.set('SpectR', arrId).then(r => console.log('done'))
                        }
                        else {
                            spectrID.push(status.ID)
                            this.storage.set('SpectR', spectrID).then(r => console.log('done'))
                        }
                    })
                    this.navCtrl.back()
                } else {
                    this.presentToast("Wrong password :/")
                }
            } else if (!this.ble.isConnected) {
                this.presentToast("Something went wrong :/")
            }
        }
        )
    }
    )