2

I'm creating nodejs server and connect it to local mongodb on EC2 instance. Connection with mongodb is error.

I've tried it on local machine and it ran well. I've tried use MongoAtlas successfully. But when try on EC2 instance, it shows the error message:

[email protected] start /home/ec2-user/workspace/rep01 node ./bin/www

mongdb://127.0.0.1:27017/telegram MongoDb connection error { MongoParseError: Invalid connection string at parseConnectionString (/home/ec2-user/workspace/rep01/node_modules/mongodb-core/lib/uri_parser.js:412:21) at connect (/home/ec2-user/workspace/rep01/node_modules/mongodb/lib/operations/mongo_client_ops.js:180:3) at connectOp (/home/ec2-user/workspace/rep01/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3) at executeOperation (/home/ec2-user/workspace/rep01/node_modules/mongodb/lib/utils.js:420:24) at MongoClient.connect (/home/ec2-user/workspace/rep01/node_modules/mongodb/lib/mongo_client.js:168:10) at Promise (/home/ec2-user/workspace/rep01/node_modules/mongoose/lib/connection.js:521:12) at new Promise () at NativeConnection.Connection.openUri (/home/ec2-user/workspace/rep01/node_modules/mongoose/lib/connection.js:518:19) at Mongoose.connect (/home/ec2-user/workspace/rep01/node_modules/mongoose/lib/index.js:270:15) at Object. (/home/ec2-user/workspace/rep01/app.js:16:10) at Module._compile (internal/modules/cjs/loader.js:689:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) at Module.load (internal/modules/cjs/loader.js:599:32) at tryModuleLoad (internal/modules/cjs/loader.js:538:12) at Function.Module._load (internal/modules/cjs/loader.js:530:3) at Module.require (internal/modules/cjs/loader.js:637:17) name: 'MongoParseError', [Symbol(mongoErrorContextSymbol)]: {} }

I tried to reinstall mongodb, but not work. Mongodb version is v4.0.5 Please help me with this. Thank you!

2
  • can you add code as well with your connection string with this error, i can only tell you which connection string you have provided for connection is not valid means there mongo not running Commented Jan 6, 2019 at 11:14
  • I used 'mongo' and it works. Here's the code: const mongoose = require("mongoose"); const mongoDb = process.env.MONGODB_URI; mongoose.connect( mongoDb, { useNewUrlParser: true } ); mongoose.Promise = global.Promise; const db = mongoose.connection; db.on("error", console.error.bind(console, "MongoDb connection error"));. Connection string: "mongodb://localhost:27017/telegram" Commented Jan 6, 2019 at 17:24

3 Answers 3

3

Try this, This works for me, I got same erros because i tried to connect in wrong way. My previous code that produce the " 'MongoParseError', [Symbol(mongoErrorContextSymbol)]: {} }" error is -

mongoose.connect('mongodb://localhost/27017/creative-ideas', { useNewUrlParser: true })
    .then(() => console.log("Mongodb connected"))
    .catch(err => console.log(err));

The corrected code is:

mongoose.connect('mongodb://localhost:27017/creative_ideas', { useNewUrlParser: true })
    .then(() => console.log("Mongodb connected"))
    .catch(err => console.log(err));
Sign up to request clarification or add additional context in comments.

Comments

0

DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Di scover and Monitoring engine, pass option { useUnifiedTopology: true } to the Mo ngoClient constructor.

so the correct code is

mongoose.connect('mongodb://localhost:27017/usercrud', { useNewUrlParser: true, useUnifiedTopology: true  })
.then(() => console.log("Mongodb connected"))
.catch(err => console.log(err));

Comments

-1

You can follow this link for instructions to connect your node.js with the MongoDB.

On example is

//with using the promise
mongoose.connect(uri,options).then(() => {
  //connection established successfully
}).catch(error){
  //catch any error during the initial connection
}

1 Comment

Link answers aren't ideal, if the link changes the answer is no longer relevant. If providing a link it's good practice to copy any meaning details into your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.