0
I am trying to create several people in my database when starting the application, but it is only creating the last user.
APP.COMPONENT
export class AppComponent implements OnInit {
title = 'webwhatsapp';
peoples: Array<People> = [
{ id: '0', name: 'THIAGO DE BONIS CARVALHO SAAD SAUD', avatar: 'assets/users/thiagobonis.jpg', messages: null },
{ id: '1', name: 'BILL GATES', avatar: 'assets/users/billgates.jpg', messages: null },
{ id: '2', name: 'STEVE JOBS', avatar: 'assets/users/stevejobs.jpg', messages: null },
{ id: '3', name: 'LINUS TORVALDS', avatar: 'assets/users/linustorvalds.jpg', messages: null },
{ id: '4', name: 'EDSGER DIJKSTRA', avatar: 'assets/users/dijkstra.jpg', messages: null },
];
constructor(public peopleService: PeopleService) {}
ngOnInit(): void {
this.peoples.forEach((people: People) => this.peopleService.create(people));
}
}
BACKEND SERVICE:
createPeople(people: People): Observable<People> {
return this.getPeople(people.id).pipe(
catchError((error) => throwError(error)),
switchMap(() => {
return this.httpClient.post<People>(this.SERVER_URL, people).pipe(
map((data) => data),
catchError((error) => throwError(error))
);
})
);
}