-
-
Save foull/0834e34ea3d9174e2c4bfe36a44a14d0 to your computer and use it in GitHub Desktop.
| var MongoClient = require('mongodb').MongoClient | |
| // | |
| // config | |
| // | |
| var mongoPort = '27017' | |
| var mongoHost = 'localhost' | |
| var dbName = 'dbName' | |
| var userName = 'admin' | |
| var userPassword = 'pass1' | |
| // | |
| // start | |
| // | |
| MongoClient.connect('mongodb://'+mongoHost+':'+mongoPort+'/'+dbName, | |
| function(err, db) { | |
| if (err){ | |
| return console.log('Error: could not connect to mongodb') | |
| } | |
| // Use the admin database for the operation | |
| var adminDb = db.admin() | |
| // Add the new user to the admin database | |
| adminDb.addUser(userName, userPassword, { | |
| roles: [{ | |
| role : "userAdmin", | |
| db : dbName | |
| }] | |
| }, | |
| function(err, result) { | |
| if (err){ | |
| return console.log('Error: could not add new user') | |
| } | |
| // Authenticate using the newly added user | |
| adminDb.authenticate(userName, userPassword, function(err, result) { | |
| if (err){ | |
| return console.log('Error: could authenticate with created user') | |
| } | |
| console.log('Ok') | |
| db.close() | |
| }) | |
| }) | |
| }) |
I'm getting this error below at line:
adminDb.authenticate(userName, userPassword, function(err, result) {
TypeError: adminDb.authenticate is not a function
at eval (eval at (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\mongo-create-user.js:42:5), :1:13)
at C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\mongo-create-user.js:42:5
at result (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:75:17)
at session.endSession (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:64:11)
at ClientSession.endSession (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\core\sessions.js:135:41)
at executeCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\execute_operation.js:59:17)
at handleCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\utils.js:129:55)
at execute (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\add_user.js:86:16)
at handleCallback (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\utils.js:129:55)
at db.s.topology.command (C:\Users\Deli\AiCAN\03_RD\GIC\LEARN_MONGODB_AND_JS\LEARN_02\kerberos\myproject\node_modules\mongodb\lib\operations\command.js:115:7)
Since the mongodb driver version 3 you cannot access the admin db directly anymore. You either have to connect using the API v3 or install the old driver.