1

I made this node js app and then i tried with postman but every time i made a request that involves mongodb, it keeps loading. The function find of the model is where the code stops and the callback is never called.

app.js

var express = require("express"), app = express(), bodyParser = require('body-parser'), methodOverride = require('method-override'), mongoose = require('mongoose'); //Connection to DB mongoose.connect('mongodb://localhost:27017/users', function(err, res) { if(err) { console.log('ERROR: connecting to Database. ' + err); } }); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(methodOverride()); var models = require('./models/user')(app, mongoose); var userContoller = require('./controllers/user'); var router = express.Router(); router.get('/', function(req, res) { console.log('GET'); res.send("Hello World!"); }); app.use(router); var users = express.Router(); users.route('/users') .get(userContoller.findAllUsers); app.use('/api', users); app.listen(3000, function() { console.log("Node server running on http://localhost:3000"); }); 

models/user.js

exports = module.exports = function(app, mongoose){ var userSchema = new mongoose.Schema({ userName: { type: String }, password: { type: Number } }); mongoose.model('User', userSchema); }; 

controllers/user.js

var mongoose = require('mongoose'); var User = mongoose.model('User'); //GET - Return all tvshows in the DB exports.findAllUsers = function(req, res) { console.log('llega'); User.find(function(err, users) { if(err) res.send(500, err.message); console.log('GET /users') res.status(200).jsonp(users); }); }; 

The mongodb server is started through the console but i don't know how to check it.

Thanks!

EDIT:

I made the code easier for me to test and solve the problem. The code now is this and im not getting the connection to mongodb.

var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var db = mongoose.connection; db.on('error', function() { console.log('error'); }); db.once('open', function() { console.log('connected'); }); 

I don't get in the console the error or the connected.

In the mongod console i get some messages saying that a new connection was made. This happens every time i open the nodejs program. Thanks

3
  • why jsonp ? Did you mean json ? Commented Dec 15, 2016 at 1:11
  • Is the express function to return a json Commented Dec 15, 2016 at 8:58
  • You can open the terminal and type mongo to check if mongo work properly. Commented Dec 15, 2016 at 9:17

2 Answers 2

1

I think the problem is that you are giving call back to the mongoose.connect function. In my case i did:

mongoose.connect(url, options) const db = mongoose.connection db.on('error', function () { console.log('Cannot Connect to DB') }) db.once('open', function () { console.log('Mongoose Connected to DB') }) 

Also instead of:

users.route('/users').get(userContoller.findAllUsers);

You may try:

users.get('/users', userController.findAllUsers);

And I realized that you don't pass a next argument to your controller which express generally complains if you dont pass.

Edit: I think i found the error.

When you are using the .find function you need to pass 2 arguments. In your case because you are not passing the callback as the second argument it never gets called.

User.find({}, callback) should do it.

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

2 Comments

I updated the answer maybe you should check it. Cheers!
I edited the question, the problem seems to be on the mongoose connection
0

I found the problem.

The version of mongoose was older than the needed to connect to my mongodb version. Just update it via npm and good to go.

1 Comment

I am also getting the same error, my call still goes infinity, connection.readystate always give 2. I did update the version now is 3.4.12. I am using linux machine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.