我目前正在使用 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.testVar
与 testVar
不是同一个变量
关于javascript - 将值从数据传递到变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56619454/