var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(
'mongodb://testUser:test@127.0.0.1:27017/testDB',
{
db: {
w: 1,
native_parser: false,
},
server: {
poolSize: 5,
socketOptions: { connectionTimeoutMS: 500 },
auto_reconnect: true,
},
replSet: {},
mongos: {},
},
function (err, db) {
if (err) {
console.log('Connection Failed Via Connection String.');
} else {
}
}
);
Asked
Active
Viewed 67 times
1

lpizzinidev
- 12,741
- 2
- 10
- 29

andrew wong
- 21
- 2
-
var MongoClient = require('mongodb').MongoClient; MongoClient.connect("mongodb://testUser:test@127.0.0.1:27017/testDB", { db: { w: 1, native_parser: false }, server: { poolSize: 5, socketOptions: { connectionTimeoutMS: 500 }, auto_reconnect: true }, replSet: {}, mongos: {} }, function(err, db) { if (err) { console.log("Connection Failed Via Connection String."); } else { }); – andrew wong Jun 09 '22 at 03:16
-
Hi Andrew, The comment can be placed in the original question by using "Edit". Could you also expand on the issue you are having? Describe in the edit what you are attempting to do and any error messages you receive. – Matt Jun 09 '22 at 04:01
-
PS C:\Users\trand\MEAN\mongo> node db_connect_string.js Connection Failed Via Connection String. PS C:\Users\trand\MEAN\mongo> – andrew wong Jun 09 '22 at 06:13
-
You should print the actual error message, not a static text "Connection Failed Via Connection String.", i.e. `console.log(err.errmsg)` or `console.log(err.message)` – Wernfried Domscheit Jun 09 '22 at 08:44
-
Maybe this: https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Jun 09 '22 at 08:45
-
i see, so i have take out the options in MongoClient.connect and it can connect now. – andrew wong Jun 09 '22 at 08:52
-
MongoClient.connect("mongodb://testUser:test@127.0.0.1:27017/testDB", function(err, db) { if (err) { console.log("Connection Failed Via Connection String."); console.log(err.errmsg); } else { console.log("Connected Via Connection String ..."); db.logout(function(err, result) { } }); – andrew wong Jun 09 '22 at 08:54
-
PS C:\Users\trand\mean\mongo> node db_connect_string2.js Connected Via Connection String ... C:\Users\trand\mean\mongo\db_connect_string2.js:12 db.logout(function(err, result) { ^ TypeError: db.logout is not a function at C:\Users\trand\mean\mongo\db_connect_string2.js:12:12 ..... – andrew wong Jun 09 '22 at 08:55
-
something wrong with db.logout ?? – andrew wong Jun 09 '22 at 08:55