Site name: missoma-monitoring
I have a scheduled function that fetches some data from an API and then pushes it to a metaobject in Shopify. This all works perfectly when running locally, but as soon as I push to Netlify and try to invoke the function, I get the following error:
Mar 17, 09:01:46 AM: INIT_START Runtime Version: nodejs:16.v12 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:734d712d91ebc245bd01c52ae85b4025a7885efd1b8c87c0ce49f3e47d2116a4
Mar 17, 09:01:46 AM: 1309d816 INFO [request] /.netlify/functions/next_api_globalecoefficientuk
Mar 17, 09:01:46 AM: 1309d816 ERROR TypeError: Cannot read properties of undefined (reading 'headers')
at Object.apiResolver (/var/task/nextPage/chunks/458.js:2946:55)
at Module.<anonymous> (/var/task/nextPage/chunks/458.js:123:30)
Mar 17, 09:01:46 AM: 1309d816 ERROR TypeError: Cannot read properties of undefined (reading 'headers')
at Object.apiResolver (/var/task/nextPage/chunks/458.js:2946:55)
at Module.<anonymous> (/var/task/nextPage/chunks/458.js:123:30)
Mar 17, 09:01:46 AM: 1309d816 ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'headers')","reason":{"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'headers')","stack":["TypeError: Cannot read properties of undefined (reading 'headers')"," at Object.apiResolver (/var/task/nextPage/chunks/458.js:2946:55)"," at Module.<anonymous> (/var/task/nextPage/chunks/458.js:123:30)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read properties of undefined (reading 'headers')"," at process.<anonymous> (file:///var/runtime/index.mjs:1188:17)"," at process.emit (node:events:513:28)"," at emit (node:internal/process/promises:140:20)"," at processPromiseRejections (node:internal/process/promises:274:27)"," at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}
Mar 17, 09:01:46 AM: Unknown application error occurred
Runtime.Unknown
Mar 17, 09:01:46 AM: 1309d816 Duration: 15.45 ms Memory Usage: 71 MB Init Duration: 276.12 ms
Mar 17, 09:01:46 AM: INIT_START Runtime Version: nodejs:16.v12 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:734d712d91ebc245bd01c52ae85b4025a7885efd1b8c87c0ce49f3e47d2116a4
I have gone through the code, rewritten it, deleted it, written it again, cried, considered my life choices, rewritten it again and still no luck. I cannot see what the problem is.
Here is the current version of the code:
export default (_req, _res) => {
(async () => {
const rawResponse = await fetch('MY API URL', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({a: 1, b: 2})
});
const content = await rawResponse.json();
console.log(content);
const graphql = JSON.stringify({
query:
"mutation metaobjectUpdate($id: ID!, $metaobject: MetaobjectUpdateInput!) {\n metaobjectUpdate(id: $id, metaobject: $metaobject) {\n metaobject {\n id\n fields {\n value\n }\n }\n userErrors {\n field\n message\n }\n }\n}",
variables: {
id: "MY SHOPIFY GID",
metaobject: {
fields: [{ key: "rates", value: JSON.stringify(content) }],
handle: "MY HANDLE",
},
},
});
const requestOptions = {
method: "POST",
headers: {
'X-Shopify-Access-Token': 'MY TOKEN',
'Content-Type': 'application/json'
},
body: graphql,
redirect: "follow",
};
fetch("MY API URL", requestOptions)
})();
return {
statusCode: 200,
body: JSON.stringify({ message: "Coeffcients have been updated!" }),
};
}
export const config = {
type: "experimental-background",
schedule: "0 3 * * *",
};
I’ve tried using other methods instead of just having two fetch requests like using .then()
and having a new Promise()
to handle the second part of updating Shopify but it didn’t make a difference.
As I mentioned above, this all works perfectly when I run it locally, it’s just when it’s built in Netlify that it fails. I’ve been through Netlify support but unfortunately, they couldn’t help resolve the issue and suggested I try here.
Thanks!