Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js Express mongoose query find

Node.js Express mongoose query find


I have a little problem with Express and mongoose using Node.js . I pasted the code in pastebin, for a better visibility.

Here is the app.js: Here is the routes/index.js:

Since the db.js isn't big, I post it here:

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

module.exports = function () {
    function(err) {
        if (err) { throw err; }

var User = new Schema({
    username: {type: String, index: { unique: true }},
    mdp: String    

module.exports = mongoose.model('User', User);

As you can see, I used the console.log to debug my app, and I found that, in routes/index.js, only the a appeared. That's weird, it's as if the script stopped (or continue without any response) when

userModel.findOne({username: req.body.username}, function(err, data)

is tried.

Any idea?

Problem courtesy of: Nicolas Chagnet


You never connect to your database. Your connect method is within the db.export, but that is never called as a Function from your app.

Also, you are overwriting your module.exports - if you want multiple functions/classes to be exported, you must add them as different properties of the module.export object. ie.:

module.export.truthy = function() { return true; }
module.export.falsy = function() { return false; }

When you then require that module, you must call the function (trueFalse.truthy();) in order to get the value. Since you never execute the function to connect to your database, you are not recieveing any data.

Solution courtesy of: Jesse Fulton


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js Express mongoose query find


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription