Question

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

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)]: {} }

Show comments

Submit an answer

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!

Sign In or Sign Up to Answer

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.

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

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.