'MongoDB Scheduled Trigger Save Error -- What to Return?

This problem is very annoying. So, I am making a scheduled trigger run every 24 hours. It simply gets items from one collection does some data processing then appends information to another collection. The functioning code works even when the function runs. But it will not let me save because there are "runtime" errors? Even though it was executed perfectly and returned.

Console Error

> result (JavaScript): 
EJSON.parse('{"$undefined":true}')

I suppose this has something to do with returning. but when I return null I get this:

> result: 
null
> result (JavaScript): 
EJSON.parse('null')

when trying to save I get this at the top of the page:

runtime error during function validation

Function Code:

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;
};


Solution 1:[1]

The answer was simple and now I feel ignorant. Instinctual I put the module imports at the top of the document. However this is incorrect and they need to be placed in the exports function, like so:

exports = function (x,y,z) {
 const http = context.http;
 return;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Exterkamp