SandboxTimeoutError in custom mongo database

Hello, I went through all possible forum posts with the same issue of: SandboxTimeoutError: Request to Webtask exceeded allowed execution time. And still no help.

  • My Nodejs tenant is set to: Node version 18
  • I tried changing MongoDB driver from 3.1.0 to 4.1.0 to 5.7.0 and even used the require('mongodb').Mongoclient without a version
  • I tried removing the code and putting there “console.log(“hello”);” and looking into the Realtime logs and nothing there.
  • I tried putting the password into encrypted configuration and using that
  • I tried using the password in the string.
  • I created the database with name “db-name” and created the “users” collection.
  • I followed the blog post tutorial to the point.

This is the configuration for create user script:

function create(user, callback) {
  const bcrypt = require('bcrypt');
  const MongoClient = require('mongodb@5.7.0').MongoClient;
  const client = new MongoClient('mongodb+srv://<username>:' + configuration.MONGO_PASSWORD + '@<url>.mongodb.net/?retryWrites=true&w=majority');

  client.connect(function (err) {
    if (err) return callback(err);

    const db = client.db('db-name');
    const users = db.collection('users');

    users.findOne({ email: user.email }, function (err, withSameMail) {
      if (err || withSameMail) {
        client.close();
        return callback(err || new Error('the user already exists'));
      }

      bcrypt.hash(user.password, 10, function (err, hash) {
        if (err) {
          client.close();
          return callback(err);
        }

        user.password = hash;
        users.insert(user, function (err, inserted) {
          client.close();

          if (err) return callback(err);
          callback(null);
        });
      });
    });
  });
}

Any help would be appreciated.

Hi, I had the same issue, you need to change your runtime from 18 to 16 and in your script change

 const MongoClient = require('mongodb@5.7.0').MongoClient;

to

 const MongoClient = require('mongodb@4.1.0').MongoClient;
1 Like

See my post in another thread. The fix is to use Promises instead of callbacks for MongoDB calls starting with Node 18 and Mongo 5.x
I can confirm this works and fixes the problem. I’m able to use latest Mongo 5.x client and Node 18.](Connecting Auth0 to MongoDB - #29 by dmitry)

Thank you!! <3 Works perfect