This situation is becoming really frustrating. I decided to set up a scheduled trigger to run every 24 hours. It essentially extracts data from one collection, processes it, and then appends the information to another collection. Interestingly, the code works perfectly fine while the function is running. However, I am unable to save the changes due to some obscure "runtime" errors showing up. This issue persists even though the function execution seems flawless and returns the expected results.
Error Message in Console:
> result (JavaScript):
EJSON.parse('{"$undefined":true}')
I suspect that this error might be related to the return statement. When I try to return null
, the following happens:
> result:
null
> result (JavaScript):
EJSON.parse('null')
Upon attempting to save, I encounter the following message at the top of the page:
runtime error during function validation
Function Implementation:
exports = async function() {
const usersCol = context.services.get("SchoologyDashCluster").db("SchoologyDashApp").collection("users");
const gradesCol = context.services.get("SchoologyDashCluster").db("SchoologyDashApp").collection("grades");
var usersCusor = await usersCol.find( ).toArray();
var gradesCusor = await gradesCol.find( ).toArray();
let insert = [];
for (let i = 0; i < usersCusor.length; i++) {
var user = usersCusor[i];
var userSavedGrades = gradesCusor[i].grades
var currentGrades = await getGrades(user.schoologyUID, user.consumerKey, user.secretKey);
var lastGraded = NaN;
let index = gradesCusor[i].grades.length - 1;
while (true) {
if (gradesCusor[i].grades[index].changed == 1) {
lastGraded = index;
break
}
index = index - 1;
}
console.log(lastGraded)
if (userSavedGrades[lastGraded].grades.ga == currentGrades.ga){
currentGrades = { changed : 0, time: new Date().getTime()};
} else {
currentGrades = {changed : 1, grades: currentGrades, time : new Date().getTime()};
}
gradesCol.updateOne(
{"user" : user._id},
{"$push" : {"grades" : currentGrades}}
)
}
// return usersCol.find( );
return null;
};