Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda