javascript - 将值从数据传递到变量

标签 javascript ionic-framework google-cloud-firestore

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

      };
    })

最佳答案

在行

if (testVar = true)

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

此外,JavaScript 中没有隐式 this,因此 this.testVartestVar 不是同一个变量

关于javascript - 将值从数据传递到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56619454/

相关文章:

javascript - html @MEDIA print {},隐藏标记,在打印中留下空白

JavaScript 多次打印

javascript - 为什么分页不起作用?

javascript - Math.Random() 选择随机数组

javascript - 在 Javascript 中读取 JSON 响应

android - ionic 角: opening links in (external) mobile browser

javascript - ionic2 从函数检索数据到 html 页面

javascript - 生成 Angular JS href

firebase - 如何使用flutter中的Map在Firestore中动态添加特定集合的文档的字段?

firebase - 在Flutter中,我们如何从特定索引的队列中拉出文档?