I am sending json data from serverless function but when when I am getting parsing error when I hit the endpoint in my react app
serverless function code
import axios from "axios";
exports.handler = async (event, context) => {
const seconds = event.queryStringParameters.seconds;
console.log("seconds: ", seconds);
// seconds = 1668153022
const targetUrl = `https://blockchain.info/blocks/${seconds}000?format=json`;
try {
const response = await axios.get(targetUrl);
console.log("RESPONSE: ", response.data);
return {
statusCode: 200,
body: JSON.stringify(response.data),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error),
};
}
};
react code
useEffect(() => {
const getData = async () => {
const url1 = `/.netlify/functions/get-data?seconds=${Math.round(
value / 1000
)}`;
console.log("URL1: ", url1);
const res = await fetch(url1);
const result = await res.json();
console.log("result: ", result);
};
if (value !== null) {
getData();
}
}, []);
I logged res
and this is what I am getting back from my serverless function
Why does await res.json()
result in a parsing error?
Update:
I even changed the response from function to hardcoded object just to get it working
like this
try {
const response = await axios.get(targetUrl);
console.log("RESPONSE: ", response.data);
// response.data
return {
statusCode: 200,
body: JSON.stringify({ name: 1 }),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error),
};
}
But still, I am getting the same error
Edit
I am getting 404 error in the network tab and and says Cannot GET /.netlify/functions/get-data
Here is my github code: https://github.com/MubashirWaheed/query-bitcoin-blockchain/tree/bug
Please can anyone help me fix this issue