【问题标题】:Passing value from data to variable将值从数据传递到变量
【发布时间】:2019-06-16 13:50:49
【问题描述】:

我目前正在使用 Ionic 4 和 Firestore。我正在尝试检查 Firestore 中是否存在文档 ID,然后根据情况执行 if-else。

但是,当我返回值并尝试将其分配给变量时,它不保存该值。这是我的示例代码

this.service.checkLogin(this.test).then((data) => {
      this.testVar = data;
      console.log(this.testVar) //here shows 'false if data is false'

      if (testVar = true) {
        console.log(this.testVar) //however, over here it will show true 
                                    even if the result above was initally false. 
        this.router.navigateByUrl('/home');
      }
      else {

      };
    })

【问题讨论】:

    标签: javascript ionic-framework google-cloud-firestore


    【解决方案1】:

    排队

    if (testVar = true)
    

    您正在分配给testVar,而不是测试它是否为真(您可以使用 == 或 === 运算符来做到这一点)

    另外,javascript 中没有隐含的this,所以this.testVar 与testVar 不是同一个变量

    【讨论】:

      【解决方案2】:

      首先,你在哪里定义你的测试变量? 我想,是this.testVar。

      那么您测试变量的方式是错误的。

      单等意味着赋值,而不是测试。

      您可以这样做,它将测试与undefined 或null 不同的所有内容

      if (this.testVar)
      

      或者如果你想测试它是否真的是一个真实的价值

      if (this.testVar === true)
      

      它将测试相等值,typeof 值(布尔值或其他)

      【讨论】:

        猜你喜欢
        • 2019-04-29
        • 2014-05-27
        • 1970-01-01
        • 1970-01-01
        • 2014-01-17
        • 1970-01-01
        • 1970-01-01
        • 2012-11-09
        • 1970-01-01
        相关资源
        最近更新 更多