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

Subscribe
Share

This comment has been deleted

mydroplet:~$ sudo ufw status Status: active

To Action From


OpenSSH ALLOW Anywhere Nginx HTTP ALLOW Anywhere 27017 ALLOW 142.93.215.18 443 ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6) 443 (v6) ALLOW Anywhere (v6)


Submit an 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.

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