Here is my JS code:
var mongoose = require('mongoose');
mongoose.connect("mongodb://myUsername:[email protected]:61039/accounttest");
and here is the error when I start server
C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongodb\lib\db.js:1774
if(databaseName.indexOf(invalidChars[i]) != -1) throw MongoError.create({message: "database names cannot contain the character '" + invalidChars[i] + "'", driver:true});
^
MongoError: database names cannot contain the character ' '
at Function.MongoError.create (C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongodb-core\lib\error.js:31:11)
at validateDatabaseName (C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongodb\lib\db.js:1774:70)
at new Db (C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongodb\lib\db.js:154:3)
at NativeConnection.doOpen (C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:55:15)
at NativeConnection.Connection._open (C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongoose\lib\connection.js:531:15)
at C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongoose\lib\connection.js:289:11
at new Promise.ES6 (C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongoose\lib\promise.js:45:3)
at NativeConnection.Connection.open (C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongoose\lib\connection.js:288:17)
at Mongoose.connect (C:\Users\Qk Lahpita\Desktop\server_node_2\node_modules\mongoose\lib\index.js:242:47)
at Object.<anonymous> (C:\Users\Qk Lahpita\Desktop\server_node_2\server.js:36:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
My database's name is "accounttest", not contain ' ' character. Why do I have this error and how can I fix it?
Thanks!
The problem is your database name itself. You should not have spaces in it.
Example
const databaseName = "Users"
const connectionURI = "mongodb://127.0.0.1"
MongoClient.connect(connectionURI,{useUnifiedTopology:true},(err,client) => {
if(err) return log("Unable to connect to database");
log("Database Connected")
const db = client.db(databaseName);
then you can do whatever you intended to Example :
db.addUser("John","kio")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With