I encountered an issue while developing a login form in the method "subscribe" under the submit function in the provided code snippet. The error message states: Property 'subscribe' does not exist on type 'void'. How can I resolve this problem? The code segment is outlined below:
LoginComponent.ts
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AuthService } from '../auth.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginForm!: FormGroup;
isSubmitted=false;
results: any = false;
constructor(private formbuilder:FormBuilder, private authService: AuthService,
private router: Router) { }
ngOnInit() {
this.loginForm = this.formbuilder.group({
// email:['', [Validators.required, Validators.email]],
name: ['',Validators.required],
password: ['',Validators.required]
});
//loginForm.controls.email
//use getter property instead
//fc.email
}
get fc(){
return this.loginForm.controls;
}
submit(){
this.isSubmitted = true;
if(this.loginForm.valid)
this.authService.authUser(this.loginForm.value.name,
this.loginForm.value.password).subscribe(data => {
//error lies in line above(underlined subscribe)
this.results = data;
if (this.results[0].auth)
{
this.authService.setSecureToken(this.loginForm.value.name);
this.router.navigateByUrl('/user');
} else{
console.log("Wrong username or password")
}
});
}
}
AuthUser function:
authUser(username: string, pw: string) {
return this.http.post<any[]>(this.authuser, {
'username': username,
'password': pw
}),
console.log('user registered!')
}