Saturday, 15 April 2017

Express.js cannot connect to mongodb

Im on OS x. Im running an express.js app. This is what I have in app.js:

var express    = require('express');
var mongoose = require('mongoose');
var cn = 'mongodb://localhost/test';
mongoose.connect(cn, function(error) {
    console.log("inside?");
    console.log(res);
});
console.log("outside"); 

If I open a terminal a write:

mongo

then I see this:

MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.3
Server has startup warnings: 
2017-04-13T16:25:17.440+0200 I CONTROL  [initandlisten] 
2017-04-13T16:25:17.440+0200 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-04-13T16:25:17.440+0200 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-04-13T16:25:17.440+0200 I CONTROL  [initandlisten] 
> show dbs
admin  0.000GB
local  0.000GB
test   0.000GB  

When I run

node app.js

The only thing I see in the terminal is:

outside

So it never goes inside "connect"? I have a callback because I realized that when I tried to communicate with database the code hangs up. I dont get any error message but it seems I never can query mongodb from express. What is wrong? I have tried several different connection string, for example 127.0.0.1 or even 127.0.0.1:27017 but no luck.



via oderfla

No comments:

Post a Comment