Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cant get data from database after multiple schema declared (mongoose + express + mongodb

I'm new to node.js and I am having problem accessing to the when multiple mongoose schema were declare.

//schema.js in model

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

//User Schema
var userSchema = new Schema({
id: ObjectId,
firstname: {type: String, require: true},
lastname: {type: String, require: true},
username: {type: String, unique: true, require: true},
password: {type: String, require: true},
role: {type: [String], require: true}
})

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

//Question Schema
var qnSchema = new Schema({
id: ObjectId,
question: {type: String, require: true},
module_id: {type: ObjectId, ref: 'Module'}
})

var Question = mongoose.model('Question', qnSchema);
module.exports = Question;

//Answer Schema
var ansSchema = new Schema({
id: ObjectId,
answer: String,
question: {type: ObjectId, ref: 'Question'}
})

var Answer = mongoose.model('Answer', ansSchema);
module.exports = Answer;

//Module Schema
var modSchema = new Schema({
id: ObjectId,
name: {type: String, require: true}
})

 var Module = mongoose.model('Module', modSchema);
  module.exports = Module;

//Role Schema
var roleSchema = new Schema({
id: ObjectId,
role: {type: String, require: true}
})

var Role = mongoose.model('Role', roleSchema);
module.exports = Role;

//index.js in controller

var mongoose = require('mongoose');
var User = require('../models/schema');
var db = mongoose.connect('mongodb://localhost/damai');

module.exports = function(app) {

app.get('/', function(req, res) {
    if (typeof req.session.userid == 'undefined') {
        res.render('login', { title: app.get('title') });           
    } else {
        res.render('index', { title: app.get('title') });       
    }
});

app.post('/login', function(req, res) {
    passwordVerification(req, res);
});
}

function passwordVerification(req, res)
{
var userid = req.param('userid');
var password = req.param('password');
User.findOne({'username': userid},{'password': 1}, function(err, cb)
{
    console.log(cb);
    if(cb!= null)
    {
        if (password == cb.password) {
            req.session.userid = userid;
            res.render('index', { title: app.get('title'), 'userid':    userid });
        } else {
            res.render('login', { title: app.get('title'), error: 'Invalid login'});
        }
    }
    else
    {
        res.render('login', { title: app.get('title'), error: 'Invalid login'});
    }
});
}

When I only have the "User Schema" in my schema.js, the database call from method "passwordVerification()" from index.js will return me the relevant password that was retrieve from the database. However, when I start adding in other schema such as "Question Schema" in schema.js, the method "passwordVerification()" will always return null.

like image 201
You Hock Tan Avatar asked Dec 13 '12 09:12

You Hock Tan


1 Answers

When exporting multiple models from a single file like you are in schema.js, you need to give each exported model its own exports field name.

For example, replace the multiple module.exports = ... lines in schema.js with this code at the end of the file that exports all models:

module.exports = {
    User: User,
    Question: Question,
    Answer: Answer,
    Module: Module,
    Role: Role
};

And then in index.js you can access the models like so:

var models = require('./schema');
...
models.User.findOne(...
like image 189
JohnnyHK Avatar answered Nov 12 '22 01:11

JohnnyHK