Auth0 Home Blog Docs

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

Below is the script for signup, whenever I tried to signup a user it gives out an error “Cannot read property ‘_id’ of null”

function create(user, callback) {
  const MongoClient = require("mongodb@3.1.4").MongoClient;
  const bcrypt = require('bcrypt');

  const uri = `mongodb+srv://${configuration.user}:${configuration.password}@clusters-bok6g.gcp.mongodb.net/database?retryWrites=true`; 
   const client = new MongoClient(uri, { useNewUrlParser: true });
   client.connect(err => {
    if (err) return callback(err);
    const users = client.db("dropInTalent").collection("users");

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

      bcrypt.hash(user.password, 10, function (err, hash) {
        if (err) return callback(err);
        user.password = hash;
        users.insert(user, function (err, inserted) {
          if (err) return callback(err);
          callback(null);
        });
      });
    });
  });
}

The error seems to relate to another script (not the one you have pasted). Please check your other scripts for usage of _id and ensure your logic is correct. The following documentation provides details on how to debug and handle errors in the custom database scripts:

I also receive this error using the default create mongodb template.

I modified it accordingly to be able to use atlas…

function create(user, callback) {
  const {MongoClient} = require("mongodb@3.1.4");
  const bcrypt = require('bcrypt');

	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.USERS_COLLECTION);

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

      console.log('hashing');
      bcrypt.hash(user.password, 10, function (err, hash) {
        if (err) return callback(err);
        user.password = hash;
        users.insert(user, function (err, inserted) {
          if (err) return callback(err);
          callback(null);
        });
      });
    });
  });
}

same problem

function create(user, callback) {
const bcrypt = require(‘bcrypt’);
const {MongoClient} = require(“mongodb@3.1.4”);
const dbUser = configuration.DB_USR;
const dbPwd = configuration.DB_PSWRD;
const dbHost = configuration.DB_HOST;
const dbName = configuration.CLIENT_DB;
const usersCollection = configuration.USER_COLLECTION;

const uri = mongodb+srv://${dbUser}:${dbPwd}@${dbHost}/test?retryWrites=true;
const client = new MongoClient(uri, { useNewUrlParser: true });

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

const db = client.db(dbName);
const users = db.collection(usersCollection);

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

});
}

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

1 Like

Thanks man a lot for the feedback, relaying it right now to the docs team! Glad you were able to figure it out!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.