2

I am using node js, and for the backend i am using mongodb

For connecting to DB, if i use the below line, it is fetching data

var db = monk('localhost:27017/nodetest2');

But if i change that to below line, its not fetching the data

var db = monk('mongodb://username:password@XXX.mongolab.com:27483/userdb');

I have created the collection with same name in both places

router.get('/userlist', function (req, res) {
    var db = req.db;
    var collection = db.get('userlist');
    collection.find({}, {}, function (e, docs) {
        res.json(docs);
    });
});
Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
  • 1
    You should start by adding error handling. I can connect to that database just fine (but I can't perform queries because I don't have the correct username/password, so queries return an authentication error). – robertklep Jul 28 '15 at 05:56
  • thanks @robertklep will do that are there any config/settings level changes that i have to do to enable this? – Vignesh Subramanian Jul 28 '15 at 06:08
  • 1
    You should first find out why the queries aren't working; using a standalone script for that is easiest; if you get any errors (the `e` variable in the query callback), log them so you get an idea what's going on (perhaps username/password mismatch). – robertklep Jul 28 '15 at 06:11
  • 1
    This is likely permissions and nothing to do with monk. Try connections from the shell and where that does not work, go back to your mongolab account and change permissions until you have something working. – Blakes Seven Jul 28 '15 at 06:57
  • Thanks robertklep, blakes Seven Its permission issue, i tried with different user id and password and it worked – Vignesh Subramanian Jul 28 '15 at 08:47

1 Answers1

0
  1. Check if your IP is whitelisted.
  2. try to connect via shell

Add an error handling.

const db = require("monk")(uri);
db.then(() => {
  console.log('Connected correctly to server')
})
Hunter
  • 3,080
  • 20
  • 23