1

I'm trying to use mongoDB for my app. I have 2 databases and use next code to connect:

var express = require("express");
var router  = express.Router();
var mongojs = require("mongojs");

//var mongo_db = 
//mongojs("mongodb://xxxxx:xxxxx@some_adress/cat_mean_db", ["tasks"]);

var mongo_db = mongojs("mongodb://xxxxx:xxxxx@localhost:3000/cat_db", 
["tasks"]);

//get all docs(pages)
router.get("/tasks", function (req, res, next ) {
mongo_db.tasks.find(function (error, tasks) {
    if(error)
        res.send(error);
    res.json(tasks);
});
});

if I use this db

var mongo_db = 
mongojs("mongodb://xxxxx:xxxxx@some_adress/cat_mean_db", 
["tasks"]);

everything is working well, but when I try to use db on localhost I got an exeption: connection 0 to localhost:3000 closed

the local db is exist and has user for shure.

2
  • Do not post passwords here please. And instead of placing links to images with code, paste the code here using the code button of the editor. Commented May 3, 2017 at 7:48
  • On the last piece of code I can see yet your password, and change the domain because it's not necessary here ;-) Commented May 4, 2017 at 6:39

1 Answer 1

3

From mongoose documentation on NPM :

Note: If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed.

You should try using 127.0.0.1 instead of localhost.

Sign up to request clarification or add additional context in comments.

2 Comments

thx for help but it doesnt work with 127.0.0.1 too, may be mongod is working on some other port, not the same as nodejs?
Ahaha, yes its great, oooh I, it really cant be on same port))) all work now i use 27017 port as wrote in mongod console

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.