Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
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;
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!