I'm trying to post data to my API which I extract from a form.
The problem is that it wont try to send the post request at all. Register.component.ts:
import { Component, OnInit } from '@angular/core';
import { RegisterService } from '../register.service';
import { FormGroup, FormBuilder, FormControl, Validators, FormArray, ReactiveFormsModule } from '@angular/forms';
import { user } from '../user'
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
public myForm: FormGroup;
public submitted: boolean;
public events: any[] = [];
constructor(private registerService: RegisterService,
private _fb: FormBuilder) { }
ngOnInit() {
this.myForm = new FormGroup({
username: new FormControl('', [<any>Validators.required, <any>Validators.minLength(5)]),
email: new FormControl('', [<any>Validators.required, <any>Validators.minLength(5)]),
org_number: new FormControl('', [<any>Validators.required, <any>Validators.minLength(5)]),
password: new FormControl('', [<any>Validators.required, <any>Validators.minLength(8)])
});
}
save(model: user, isValid: boolean){
this.submitted = true;
this.registerService.postUser(model)
console.log(model, isValid)
}
And the service that it's calling:
import { Injectable } from '@angular/core';
import { user } from './user';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators'
import { HttpClient, HttpHeaders } from '@angular/common/http';
const HttpOptions = {
headers: new HttpHeaders({ 'Content-type': 'application/json' })
};
const HttpOption2 = {
headers: new HttpHeaders({ 'Content-type': 'application/x-www-form-urlencoded' })
}
@Injectable({
providedIn: 'root'
})
export class RegisterService {
private usersUrl= 'api/v1/accounts';
constructor(
private http: HttpClient,
) { }
getUsers(): Observable<user[]> {
return this.http.get<user[]>(this.usersUrl)
}
postUser(object): Observable<user[]>{
console.log(object)
console.log('Did it enter this?')
return this.http.post<user[]>(this.usersUrl, user, HttpOptions)
}
}
My code outputs the expected information: username:'username', email:'email' etc.
And I know it calls the function since it also outputs:'Did it enter this?'.
My API isn't receiving a post-request and I have no idea why. I've been searching for answers on the official angular Httpclient pages, as well as numerous Stack overflow posts, I've tried adding .subscribe but I get the error type subscription is not assignable to type observable user, if i add .map() I get that map does not exist on type observable user, and if i add .pipe() I get no error message, but still no Post-request. Whereas if i add .pipe( catchError(this.handleError('addHero', hero)) ); I get type observable user is not assignable to type observable user. Any ideas?
You need to subscribe to the request to get invoked,
this.registerService.postUser(model).subscribe(result => this.result =result);
make sure to declare a variable named result to assign the responded data,
result : any;