0

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.

Browser's console after adding two videos to the list. 1st entry is a console log of the array, 2nd entry is the Json reply

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

4
  • 1
    What do you mean with "not being sent"? Is the payload not visible in the network tab? or is the request not being sent at all? Your isolated code seems to work fine for me Commented Jul 31, 2024 at 19:17
  • 1
    you should create type, interface or class to hold the data you are sending. That way you'll have the name/value pairs that JSON is expected to have. any[] is pretty broad. Debug the backend and/or check what form it's expecting the data to be in. Something seems to be sending back those custom error messages. Commented Jul 31, 2024 at 19:24
  • tutorialAdded is the array of tutorials (a bit of naming there) due to const 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? Commented Jul 31, 2024 at 23:20
  • @MafeGamboa Please share the backend code, also share the postman screenshot and network tab screenshot of this API call Commented Aug 1, 2024 at 4:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.