The error "SyntaxError: Unexpected end of JSON input" typically occurs when you try to parse an invalid or incomplete JSON string using JSON.parse(). In your case, it seems that the JSON data you are trying to parse is incomplete or missing in the request body when you access the API from one of your components.
To troubleshoot the issue, you can try logging the entire req.body object to the console to see what data is being passed in the request body in both cases. You can also check the Content-Type header of the request to ensure that it is set to application/json and that the data being passed is indeed valid JSON.
If you find that the JSON data is missing or incomplete in one of the cases, you can check the code that is making the API request to ensure that it is passing the correct data. If the data being passed is not valid JSON, you may need to modify your API route to handle the data in a different format.
Alternatively, you can try using a package like body-parser to parse the request body automatically for you, instead of using JSON.parse(). This can simplify your code and ensure that the data is parsed correctly in all cases.