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
mongoto check if mongo work properly.