2

This is the tasks.js code I'm trying to run:

/*jslint node:true*/

var express = require('express');
var router = express.Router();

var mongojs = require('mongojs');
var db = mongojs('mongodb://localhost:27017/tasks', ['tasks']);

router.get('/tasks', function (req, res, next) {
'use strict';
db.tasks.find(function(err, tasks) {
   if(err){
       res.send(err);
   }
   res.json(tasks);
});
});

module.exports = router;

The code is meant to query and display all the contents of the json file.

When I replace the db localhost URL with this mLab URL:

var db = mongojs('mongodb://username:password@ds161008.mlab.com:61008/mytasklist_muhab', ['tasks']);

It works perfectly.

I assume there is a problem with the string. I looked up the connectionString standards in MongoDB docs and I couldn't locate the problem.

I haven't assigned any username or password to the local database.

Mongod is running fine and I am able to run commands on the same database using the Mongo shell without any problem.

1 Answers1

0

According to mongojs documentation you may no need to use mongodb://localhost:27017 as part of your connectionString for local db can try by only dbName

like:

var db = mongojs('tasks', ['tasks'])

or

var db = mongojs('tasks')
var mycollection = db.collection('tasks')

and checked your connection established or not by using error or connect event

var db = mongojs('tasks', ['tasks'])

db.on('error', function (err) {
    console.log('database error', err)
})

db.on('connect', function () {
    console.log('database connected')
})
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68