Home > Back-end >  How does if else works?
How does if else works?

Time:10-21

I'm trying to figure out where my problem comes from in my algorithm.

I am trying to give the information about the connection status of a data sender with its data table.

I have translated it like this: if new data is received ( new_id different from id_from_last_request) then I set the connection status to "connected" otherwise I set it to "disconnected"

<script>
export default {
  data() {
    return {
      search: '',
      tag_id: ['bts_d02c2b7d9098aaa2', 'bts_c077ffaa9098aaa2'],
      headers: [
        {
          text: 'Tags',
          align: 'start',
          sortable: false,
          value: 'name',
        },
        { text: 'wifi', value: 'wifi' },
      ],
      val_ia: 0,
      desserts: [],
      id_memory: [],
    }
  },
  mounted() {
    this.CreateTable();
    setInterval(this.getDatafor, 1000)
  },
  methods: {
    CreateTable() {
      for (let i = 0; i < this.tag_id.length; i  ) {
        this.desserts.push(
          {
            name: this.tag_id[i],
          },
        )
      }
    },

    async getDatafor() {
      for (let i = 0; i < this.desserts.length; i  ) {
        this.val_ia = i;
        await Promise.all([this.getAllData()]);
      }
    },

    async getAllData() {
      const tag_id_name = encodeURIComponent(this.tag_id[this.val_ia]);
      const url = this.$api.getRESTApiUri()   `/all/last_id/${tag_id_name}`;
      return fetch(url)
        .then(res => res.text())
        .then((result) => {
          console.log(tag_id_name)
          console.log(this.id_memory[this.val_ia]);
          console.log(data[0].id)
          const b = this.Test(this.id_memory[this.val_ia], data[0].id);
          console.log(b)
          if(b){
            this.desserts[this.val_ia].wifi = 'connecté'
            console.log('connecté')
          }else{
             this.desserts[this.val_ia].wifi = 'déconnecté'
            console.log('déconnecté')
          }
          this.id_memory[this.val_ia] = data[0].id
        })
        .catch((error) => {
          console.log(error)
        });
    },
    Test(x, y) {
      const a = x !== y
      return a
    },
  }
}
</script>

Only in case I have no new data const b = false

here is my console:

enter image description here

I should have the disconnected status only it shows me the connected status

enter image description here

There should be a logical explanation to it but I can't see it..

CodePudding user response:

You are using equality without type coersion (x !== y) in your Test method.

Probably this.id_memory[this.val_ia] and data[0].id have different types - one is number, second one is string or otherwise.

The best solution is to convert those values to the same type before comparing like so:

Test(x,y){
    return String(x) !== String(y)
}

Some use cases:

'123' === 123 // false
'123' == 123 // true

CodePudding user response:

When creating my table, I forgot to push variables wifi and bluetooth so they did not update themselves.

CreateTable(){
        for(let i = 0; i < this.tag_id.length; i  ){
          this.desserts.push(
            {
            name: this.tag_id[i],
            wifi: 'déconnecté',
            bluetooth: 0,
            tension: 0,
            courant: 0,
            temperature: 0,
            acceléromètre: 0,
            pression_sys: 0,
            pression_dias: 0,
            frequence_cardiaque: 0,
            taux_oxygène: 0,
          },
          )
        }
      },
  • Related