简体   繁体   English

JavaScript如果未返回true

[英]Javascript if does not return true

I have a weird javascript issue. 我有一个奇怪的JavaScript问题。 I am using vue js and axios to get some data from an API. 我正在使用vue js和axios从API获取一些数据。 I have dumped console.log and the values are there but my if statement will not be true. 我已经转储了console.log并且值在那里,但是我的if语句将不正确。

Removed stuff so the code example is smaller. 已删除内容,因此代码示例较小。

See inline comments. 查看内联注释。

HTML:

<input v-model="system_id">
<button v-on:click="getSystemData" class="btn btn-primary">Get system data</button>


Data model:

data: () => ({
  errors: [],
  system_id: null,
  system_data: null
}),



Function:

getSystemData () {
    HTTP.get('/api/get_systems')
    .then(response => {
      this.response = response.data

      // equals 1234
      console.log(this.system_id)
      for (var key in this.response) {

        // the id 1234 is found here in a large list
        console.log(this.response[key].system_id)


        // Does not go true?!?!
        if (this.response[key].system_id === this.system_id) {
          this.system_data = this.response[key].system_data
        }
      }
    })
    .catch(e => {
      this.errors.push(e)
    })
  }

Why does the if never trigger?? 为什么如果永不触发?

The problem maybe with mismatch datatypes. 问题可能出在数据类型不匹配上。 === operator will return false for if(1234==="1234"). ===运算符将为if(1234 ===“ 1234”)返回false。 Use == operator 使用==运算符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM