I'm getting the above error and I'm stumped as to why.
To explain what I'm doing,
I need to fetch data from one API and then send it to another. This is being used as a background-function job in Netlify and is held on a NextJS site.
I thought it was that I wasn't handling the response correctly so I added them in (as you can see in the code, return res.status(200).json({ success: true, result: result });
for example. But that didn't change anything.
Then I thought maybe it was because I wasn't doing anything with the req
in my function, but I'm not sure what to do with that.
It's important to note that this runs locally using netlify-cli
with absolutely no problems at all, the whole thing seems to work perfectly, it's only when I deploy it to production that I get this error.
The code:
export default async (req, res) => {
const coefficientRequest =
"someURL";
const headers = new Headers();
headers.append("Accept", "application/json");
headers.append("Content-Type", "application/json");
fetch(coefficientRequest, {
headers: headers,
method: "POST",
body: JSON.stringify({ a: 1, b: 2 }),
}).then((response) => {
return response
.json()
.then((data) => {
const myHeaders = new Headers();
myHeaders.append(
"someToken",
"someToken"
);
myHeaders.append("Content-Type", "application/json");
const graphql = JSON.stringify({
query:
"someQuery",
variables: {
id: "someID",
metaobject: {
fields: [{ key: "rates", value: JSON.stringify(data) }],
handle: "someHandle",
},
},
});
const requestOptions = {
method: "POST",
headers: myHeaders,
body: graphql,
redirect: "follow",
};
return new Promise(() => {
fetch(
"someURL",
requestOptions
)
.then((response) => {
return response.text();
})
.then((result) => {
return res.status(200).json({ success: true, result: result });
})
.catch((error) => {
return res.status(500).json({ error: error });
});
});
})
.catch((err) => {
return res.status(500).json({ error: err });
});
});
};
Your help/advice is very much appreciated. Many thanks.
I was never able to solve this problem so I went with another solution.
With PowerAutomate I was able to create a scheduled CRON job running the tasks I needed.