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: