I am creating a function that makes two calls to a postgres database. One of the queries is not behaving as intended, returning a pending promise rather than waiting for the value of the resolved promise, despite being an awaited function. Here is the code:
const getClassSummary = async (classID) => {
const sqlText = `SELECT * FROM "public".${classID} LEFT JOIN "public"."students" ON "public".${classID}."students" = "public"."students"."id"`;
const classInfo = await pool.query(sqlText);
let marksData = classInfo.rows.map(async (entry) => {
let assignmentMarks = [];
if (entry.assignments) {
assignmentMarks[`${entry.assignments}`] = [];
let marks = await getAssignmentMarks(classID, entry.assignments);
marks.forEach((datum) => {
if (datum.score) {
assignmentMarks[`${entry.assignments}`].push(datum.score);
}
});
}
return assignmentMarks;
});
console.log(marksData);
return classInfo.rows;
};
The problem is that my console.logged marksData variable contains pending promises, rather than waiting for the getAssignmentMarks function to resolve and the returned value to be manipulated. I'm lost on how I can change my code to achieve the desired result.