首页 > 解决方案 > 将值从数据传递到变量

问题描述

我目前正在使用 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 {

      };
    })

标签: javascriptionic-frameworkgoogle-cloud-firestore

解决方案


在行

if (testVar = true)

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

this此外, javascript中没有隐式,所以this.testVar不是同一个变量testVar


推荐阅读