Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

386
Views
Property 'subscribe' does not exist on type 'void | Observable<User>'. ts(2339)
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)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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',
      };
}


about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!