简体   繁体   中英

Auth0 - How to use mongoDB on localhost with Auth0

I created a new database connection on Auth0, called local-mongo and I'm trying to use the Create script in order to create a user on my local mongoDB but I keep getting this Socket Hang Up error:

[Error] Error: socket hang up
at createHangUpError (_http_client.js:211:15)
at Socket.socketOnEnd (_http_client.js:303:23)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:913:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickDomainCallback (node.js:397:17)

The Create script template on Auth0 is as follows:

function create (user, callback) {
   mongo('mongodb://localhost:27107.com/my-db', function (db) {
   var users = db.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);
    });
  });
});
});
}

Do I need an ngrok in order to connect to the local DB or is there another way I'm missing here ? Any help would be greatly appreciated.

On basis the above script works, and your question is how to connect with localhost then the answer is yes.

You might just wish to use a mongo cloud service provider if that suits your situation since then you will automatically have cloud access. Or just install on an Amazon EC2 instance and set up security group to allow access on the required port etc.

But yes, fundamentally these are your options. There needs to be a network connection to your database server. Another method is to put an API between your Custom DB Script and the backend DB.

For an example of a Node.js sample that does this see here:

node api sample

and for custom DB scripts see here:

postgres custom db scripts

You could modify the above to use mongo instead. Just an idea only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM