字符串比较不按预期工作。

huangapple go评论98阅读模式
英文:

String Comparison does not work as intended

问题

我正在进行一个与Firebase连接的Ionic web项目。

我从Firestore中检索了数据,并使用它来确定用户是否是管理员或基金经理,但字符串比较函数不起作用。

以下是我的TypeScript代码。

role: string = "";
admin: boolean = false;
fm: boolean = false;

constructor(private authService: AuthService, private router: Router,) { }

ngOnInit() {
  this.fname = "";
  this.lname = "";
  this.nric = "";
  this.role = "";
  this.admin = false;
  this.fm = false;
  this.user = this.authService.getCurrentUser();
  this.getProfile();
  if (this.role === "Admin") {
    console.log("Admin");
    this.admin = true;
  }
  else {
    if (this.role === "Fund Manager") {
      console.log("Fund Manager");
    }
  }
}

this.getProfile() 会填充 this.role。当 this.role 为 "User" 时,Admin 变为 true,我不确定是什么原因导致了问题。

我的代码是否存在任何问题?

英文:

I am working on an Ionic web project connected to firebase.

I retrieved data from firestore and use it to determine if the user is an admin or fund manager, but the string comparison function does not work.

Here is my typescript code.

role: string = "";
  admin: boolean = false;
  fm: boolean = false;
  
  constructor(private authService: AuthService, private router: Router,) { }

  ngOnInit() {
    this.fname = "";
    this.lname = "";
    this.nric = "";
    this.role = "";
    this.admin = false;
    this.fm = false;
    this.user = this.authService.getCurrentUser();
    this.getProfile();
    if (this.role = "Admin") {
      console.log("Admin")
      this.admin = true;
    }
    else {
      if (this.role = "Fund Manager") {
        console.log("Fund Manager")
      }
    }

this.getProfile() would fill up this.role. When this.role is "User", Admin becomes true, and I am not sure what is causing the problem.

Could there be any problems with my code?

答案1

得分: 1

你可以使用 =====。但 === 运算符(严格比较)不会自动转换类型,所以我认为这样更好:

if (this.role === "Admin") {
    // 做一些事情
}

另外:switch case 语句也使用严格比较,值必须是相同的类型才能匹配:

switch (this.role) {
    case 'Admin':
        // 做一些事情
        break;
    case 'Manager':
        // 做其他事情
        break;
}
英文:

You can use === or ==. But === operator (strict comparasion) will not auto-cast type, so I think it is better:

if (this.role === "Admin") {
    // do something
}

Addition: switch case statement also use strict comparison, values must be the same type to match

switch (this.role) {
    case 'Admin':
        // do something
        break;
    case 'Manager':
        // do others
        break;
}

答案2

得分: 0

使用比较运算符 == 而不是赋值运算符 =

if (this.role == "Admin") {
  console.log("Admin")
  this.admin = true;
}
else {
  if (this.role == "Fund Manager") {
    console.log("Fund Manager")
  }
}
英文:

> use comparison operator == rather than assignment operator =

if (this.role == "Admin") {
  console.log("Admin")
  this.admin = true;
}
else {
  if (this.role == "Fund Manager") {
    console.log("Fund Manager")
  }

答案3

得分: 0

= 不同于 ==(===)。前者是将一个值赋给一个变量,而后者是进行你想要的比较。所以这里 this.role = 'admin' 总是为真。

英文:

= is different from ==(===).The formal is to assign a value to a variable, the latter is to make a comparison what you want.So here this.role = 'admin' is always true.

答案4

得分: 0

将任何值赋给变量使用 =
比较两个值而不考虑它们的数据类型使用 ==
比较两个值以及变量的数据类型使用 ===

使用 ===== 来比较两个变量

if (this.role == "Admin"){

   // 在这里编写您的逻辑

}

或者

if (this.role === "Admin"){

    // 在这里编写您的逻辑
}
英文:

To assign any value to the variable use =.
To compare two values irrespective of their data type use ==
To compare two values along with variables data type use ===

Use == or === to compare two variables

if ( this.role == "Admin"){
   
   //your logic here

}

or

if ( this.role === "Admin"){

    //your logic here
}

答案5

得分: 0

你不是在比较,而是在赋值。
除了已经提供的答案之外,你可以使用YODA条件的风格来避免这样的错误。如果你像这样编写代码:if( "Admin" = this.role),将会自动导致错误,在你的浏览器控制台中显示。

英文:

You don't compare, you're assigning a value.
In addition to the already given answers: You can use YODA Condition style to avoid mistakes like this. If you write the code like if( "Admin" = this.role) would automatically cause an error which will be shown in the console of your browser.

huangapple
  • 本文由 发表于 2020年1月6日 17:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定