I'm making an app with some videos and a "Add to watch later" button in each, which will save the selected video's title, link and a boolean, then store them in an Array and send to the backend via POST.
The request is working fine from Postman, but I'm not being able to figure it out from the frontend. On the console I can see that the info is being saved properly into the array, but it's not being sent.
HTML
<button id="element1" (click)="addToList('Video title', '/videoLink', true)">Read Later</button>
TS
export class CardComponent {
watchLaterService = inject(WatchLaterService);
watchLaterList: any[] = [];
addToList(title: string, link: string, status: boolean) {
this.watchLaterList.push({ link, title, status });
const tutorialAdded = this.watchLaterList;
this.watchLaterService.addToList(tutorialAdded).subscribe((res: any) =>{
console.log("Added!: ", this.watchLaterList);
})
};
}
Watch Later Service
API_URL = "http://localhost:3000";
addToList(tutorialAdded: any){
return this.httpClient.post(this.API_URL + "/watchLater", tutorialAdded)
}
Tweaked the code extensively
tutorialAddedis the array of tutorials (a bit of naming there) due toconst tutorialAdded = this.watchLaterList. Then you're sending that array as body in your POST request. However usually in a POST request you create just 1 entity. So maybe you actually want to:const tutorialAdded = { link, title status }. Then, push that:this.watchLaterList.push(tutorialAdded). And eventually send that JSON in the POST request?