javascript - 将值从数据传递到变量
问题描述
我目前正在使用 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
,而不是测试它是否为真(您可以使用 == 或 === 运算符来做到这一点)
this
此外, javascript中没有隐式,所以this.testVar
不是同一个变量testVar
推荐阅读
- windows - 无法在附加模式下打开文件:无法打开流:权限被拒绝
- php - 如何将标签网址排除在正则表达式中?
- java - 在Android中更改按钮的背景颜色
- database-replication - 用于 DC 到 DR 复制的最佳 SymmetricDBS 架构
- linux - Linux 上的串口标志 CRTSXOFF 在哪里?
- python - Python - 在第一次出现不允许的字符时拆分字符串
- python - 打印最佳截止值错误/类型错误:不支持的操作数类型
- scala - 使用 Aux-pattern 对 type-member 进行模式匹配
- javascript - 条件渲染未按预期工作
- sql-server - 使用凭据管理器通过 VPN 进行 SQL Server Windows 身份验证