I have a function that returns and axios promise with the data that I need to work with. After getting the data from getUsersQA()
function, I have a promise chain in order to promise the data and send the return message. The code works when I invoke the function using an event (Post Method). But when I setup this function with a scheduled cron on netlify serverless functions I am getting this output.
Feb 16, 02:55:01 AM: 2f35893d INFO Fetching Users...
Feb 16, 02:55:01 AM: 2f35893d Duration: 94.40 ms Memory Usage: 73 MB Init Duration: 370.93 ms
Immediately upon invoking getUsersQA() the function is returning for some reason when it is invoked with an event the code works.
export async function handler(event: APIGatewayEvent, context: Context) {
// Fetching Slack Users
const users = await getSpaceUsers(client);
// Fetching QA Data & Pushing Slack Message
const period = computePeriods();
let blocksContent: any[] = [];
const qaData = await getUsersQA(period.lastMonth);
for (const puser of qaData) {
// FIREBASE LOGIC
if (puser.avg_score < 85) {
const block = userQAInfo(puser, users?.get(puser.email));
blocksContent.push(block);
}
}
try {
// Call the chat.postMessage method using the WebClient
const result = await client.chat.postMessage({
channel: "",
text: ``,
blocks: blocksContent,
});
console.log(result);
return {
statusCode: 200,
};
} catch (error) {
console.error(error);
return {
statusCode: 200,
};
}
}
module.exports.handler = schedule("*/5 * * * *", handler);
This is my getUSersQA asyn function.
export async function getUsersQA(period: string[]): Promise<playvoxUser[]> {
return axios({})
.then((response) => {
let playvoxStats: playvoxUser[];
playvoxStats = response.data.result;
formatPlayvoxResponse(playvoxStats);
let lowQAStats: playvoxUser[] = [];
playvoxStats.forEach((user) => {
user.avg_score =
Math.round((user.avg_score + Number.EPSILON) * 100) / 100;
lowQAStats.push(user);
});
return lowQAStats;
})
.catch((err) => {
console.log("ERROR while fetching USERS");
return err;
});
}