Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

382
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda