Report this

What is the reason for this report?

How to connect mongodb with nodejs in same (single) droplet in digitalocean

Posted on July 6, 2019

I tried to connect mongodb and node server api app (Request URL https://services.suriyashopping.com/api/menu-list) in same droplet. The nodejs server app and mongodb running in single droplet and server placed in sub domain. I have mention my db connection code below and i got conncetion success while run server by “nodemon app.js”. But after then i made request from client to server, then i got some not authorized error. One more query is why DB and related collections not generated automatically while run server via Ubuntu terminal

MongoDB connection code


mongoose.connect('mongodb://user:pwd@127.0.0.1:27017:27017/SURIYAShoppingDB', { useNewUrlParser: true }, (err, res) => {
  if(err) {
    console.log(err);
  }
  else {
    console.log('MongoDB connection succeeded.');
  }
})

Error while request server

{ MongoError: not authorized on SURIYAShoppingDB to execute command { find: “maincategories”, filter: { status: “true”, active_status: “true” }, projection: { __v: false, catgoryImage: false, active_status: false, status: false, createdAt: false, updatedAt: false }, returnKey: false, showRecordId: false } at Connection.<anonymous> (/var/www/services.suriyashopping.com/html/node_modules/mongodb-core/lib/connection/pool.js:443:61) at Connection.emit (events.js:198:13) at processMessage (/var/www/services.suriyashopping.com/html/node_modules/mongodb-core/lib/connection/connection.js:364:10) at Socket.<anonymous> (/var/www/services.suriyashopping.com/html/node_modules/mongodb-core/lib/connection/connection.js:533:15) at Socket.emit (events.js:198:13) at addChunk (_stream_readable.js:288:12) at readableAddChunk (_stream_readable.js:269:11) at Socket.Readable.push (_stream_readable.js:224:10) at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) ok: 0, errmsg: ‘not authorized on SURIYAShoppingDB to execute command { find: “maincategories”, filter: { status: “true”, active_status: “true” }, projection: { __v: false, catgoryImage: false, active_status: false, status: false, createdAt: false, updatedAt: false }, returnKey: false, showRecordId: false }’, code: 13, name: ‘MongoError’, [Symbol(mongoErrorContextSymbol)]: {} }



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
0

Accepted Answer

Hello,

It looks like that in your MongoDB connection string you’ve added the port twice. The port should be specified only once, here’s an example:

mongodb://DbUser:DbPass@localhost:27017/SURIYAShoppingDB

If this does not solve the problem for you, could you provide me with your mongo config file so I could advice you further?

Regards, Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.