Skip to main content
Solved

SyntaxError in fetch external API

  • November 30, 2023
  • 2 replies
  • 26 views

Forum|alt.badge.img+4

Hello there,

I'm trying to fetch data from a product in one base, then send that information to an external API and then update that new info in other base. The code goes like this:

 

//input of modified product let inputConfig = input.config(); //JSON creation let body = { "costo_ult": inputConfig.costo_ult[0], "disponible": inputConfig.disp[0], "dias_obs": inputConfig.dias_obs[0] } console.log(body); console.log(JSON.stringify(body)); //Send JSON to API try { let response = await fetch(`http://167..../${inputConfig.id[0]}`, { method: 'PATCH', body: JSON.stringify(body), headers: { 'Origin': '', 'Content-Type': 'application/json ; charset=utf-8', 'Authorization': 'eyJhb...', }, }); console.log(await response.json()); } catch (error) { console.log(error); }

 

The fields I want to update are related to costs, quantities and rotation days, and the response from the script is

  1. Error {name: "SyntaxError"}
    1.  name: "SyntaxError"

 

So, can anyone explain me why this is happening and how to solve it? Thanks!

Best answer by marks

Hi Richard - Have you tried hard-coding values (instead of referencing `inputConfig`) to see if that helps troubleshoot?


When I did that, and tested with an endpoint from https://webhook.site/ that returned proper JSON, I did not get an error. You can also try changing your `await response.json()` to `await response.text()` to see if perhaps the issue is coming up since your endpoint is not returning JSON as you expected.

2 replies

Forum|alt.badge.img+13
  • Inspiring
  • Answer
  • December 1, 2023

Hi Richard - Have you tried hard-coding values (instead of referencing `inputConfig`) to see if that helps troubleshoot?


When I did that, and tested with an endpoint from https://webhook.site/ that returned proper JSON, I did not get an error. You can also try changing your `await response.json()` to `await response.text()` to see if perhaps the issue is coming up since your endpoint is not returning JSON as you expected.


Forum|alt.badge.img+4
  • Author
  • New Participant
  • December 3, 2023

Hello there @marks and thanks for the reply!

As you suggested, I tried hard-coding values and I discovered that the resource was missing while fetching the API's link. Everything's working fine now!