Multiple connection error when using node js and mongodb
<p>I'm using the following schema located in my <code>/routes/schema.js</code> file...</p>

<pre><code>var mongoose = require('mongoose');


var Schema = mongoose.Schema;

var userSchema = new Schema({
    username: String,
    password: String,
    name: String,
    last: String,
    year: Number,
    degree: String

});

var User = mongoose.model('User', userSchema);

module.exports = {
    User: User
}
</code></pre>

<p>In my <code>/routes/register.js</code> file I am storing some information using POST data...</p>

<pre><code>var User = require('../routes/schema').User; 

exports.postRegister = function (req, res) {
    var u = new User({
        username: req.body.reg_username,
        password: req.body.reg_password,
        name: req.body.reg_name,
        last: req.body.reg_lastname,
        year: req.body.reg_year,
        degree: req.body.reg_degree
    });

    u.save(function (err) {
        if (err) {
            throw err;
        }
        else {
            console.log("saved");
            res.render('index', { title: 'Express' });

        }
    });
}
</code></pre>

<p>Everything gets saved fine in my Database. But now, this <code>register.js</code> file redirects the user back to <code>/routes/index.js</code> where the user must then sign in using some credentials stored in the database.</p>

<p>So in my <code>index.js</code> file I need to check if username and password exist together in a collection in my database, I tried the following...</p>

<pre><code>var User = require('../routes/schema').User; 

exports.signin = function (req, res) {
    User.findOne({
        username: req.body.log_username,
        password: req.body.log_password
    }, function (err, docs) {
        if (docs.length) {
            console.log("name exists");
        }
        else {
            console.log("no exist");
        }
    });
};
</code></pre>

<p>I used the <code>findOne</code> function with the same Schema to check if username and password exist in a collection in the database, but it doesn't work properly. I seem to get a multiple connection error and I do not know how to avoid it. </p>

<p>When I try to login using some credentials already in the database, the console prints out <code>no exist</code> meaning the else statement in <code>exports.signin</code> is reached.</p>