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

July 6, 2019 112 views
DigitalOcean Ubuntu 16.04

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", activestatus: "true" }, projection: { _v: false, catgoryImage: false, activestatus: 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/nodemodules/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 (streamreadable.js:288:12)
at readableAddChunk (streamreadable.js:269:11)
at Socket.Readable.push (streamreadable.js:224:10)
at TCP.onStreamRead [as onread] (internal/streambasecommons.js:94:17)
ok: 0,
errmsg:
'not authorized on SURIYAShoppingDB to execute command { find: "maincategories", filter: { status: "true", activestatus: "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)]: {} }

2 comments
  • 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)

  • 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

Be the first one to answer this question.