Custom Database (MongoDB Altas) script template In signup user doesn't get created get an error

I figured it out dude… its because we are following the auth0 mongo integration guide that was posted a few months back but it differs from the mongo template scripts.

Auth0 should be paying me to figure out their damn bugs here.

Your getUser script needs to look like this:

function getByEmail(email, callback) {
  const {MongoClient} = require("mongodb@3.1.4");

  const uri = `mongodb+srv://${configuration.DB_USER}:${configuration.DB_PASSWORD}@${configuration.DB_HOST}/test?retryWrites=true`;
  const client = new MongoClient(uri, { useNewUrlParser: true });

  client.connect(err => {
    if (err) return callback(err);

    const users = client.db(configuration.DB_NAME).collection(configuration.DB_USER_COLLECTION);

    users.findOne({ email: email }, function (err, user) {
      if (err) return callback(err);
      if (!user) return callback(null);
      
      return callback(null, {
        user_id: user._id.toString(),
        nickname: user.nickname,
        email: user.email
      });
    });
  });
}

Notice how there is an added check to the template to ensure the user is not null:

if (!user) return callback(null);

2 Likes