login() {
this.auth.login(this.userName, this.password).subscribe((_: any) => {
this.router.navigateByUrl('app', {replaceUrl: true});
});
}
I dont know why i get this error, tried a few things but nothing works.
This here is my auth.login method - don’t know where the error could be - I am pretty new to Angular:
login(username: string, password: string) {
let userObject: User;
if (username === 'user' && password === 'user1234') {
userObject = {
name: 'Max Mustermann',
userRole: 'USER',
};
}
else if (username === 'admin' && password === 'admin1234') {
userObject = {
name: 'Erika Mustermann',
userRole: 'ADMIN',
};
}else{
return this.assertNever();
}
return of(userObject).pipe(
tap(user => {
Storage.set({ key: TOKEN_KEY, value: JSON.stringify(user) });
this.currentUser.next(user);
})
);
}
When deleting the assertNever() method, I get this error at the last return:
Variable 'userObject' is used before being assigned. ts(2454)
CodePudding user response:
Adding some return types to your functions will help you in the future. From what I can see, your function this.assertNever(); always throws but is declared (or inferred) to return void.
Change its declaration to return never and TS will recognize that this function will not return.
void = returns, but does not return a value never = will not return / return vaue will not be observable
CodePudding user response:
You get that error because the your 'login' method should has a path which does not have a "return".
The others paths indeed should be returning an "Observable", which sounds good.
EDIT:
Now that I can see your code, it seems that the error locates in this line:
return this.assertNever();
Could "assertNever()" method return "void"/null/nothing???
Any way, in order to avoid the error, could you fake a dummy user and return it changing this line:
else {
\\return this.assertNever();
userObject = {
name: 'Fake',
userRole: 'NOLOGGED',
};
}
