can't understand where is the problem on this typescript code.
error on line 5 ".subscribe((response: { Token: string }) => {"
login() {
this.httpClient
.post('http://localhost:4000/signin', this.loginForm.value)
.subscribe((response: { Token: string }) => {
if (response.Token === undefined) {
this.autenticazioneFallita = true;
} else {
this.router.navigate(['customer']);
}
});
}
CodePudding user response:
That's because of type only string (read comment)
login() {
this.httpClient
.post('http://localhost:4000/signin', this.loginForm.value)
.subscribe((response: { Token: string }) => {
// Here you're declaring Token as string but then your checking if it's undefined and for typescript that would be always false
if (response.Token === undefined) {
this.autenticazioneFallita = true;
} else {
this.router.navigate(['customer']);
}
});
The solution is to declare Token as string or undefined:
.subscribe((response: { Token?: string }) => { /* ... */ })
By adding ?
before the type typescript will know that that variable/attribute will be the type you provided OR undefined
CodePudding user response:
Maybe try with parameter to define type?
login() {
this.httpClient
.post<{Token?: string}>('http://localhost:4000/signin',
this.loginForm.value)
.subscribe((response => {
if (response.Token === undefined) {
this.autenticazioneFallita = true;
} else {
this.router.navigate(['customer']);
}
});