斜面声明的多重模式(猫鼬+快递+ MongoDB的后从数据库中获得数据(cant get data

2019-08-04 01:15发布

我是新来的Node.js,我在访问的时候多猫鼬模式是申报的问题。

在模型//schema.js

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

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'});
    }
});
}

当我只有在我的schema.js“用户模式”,从方法数据库调用“passwordVerification()”从index.js将返回我这是从数据库中检索相关密码。 然而,当我开始添加在其他模式,如“问题模式”中schema.js,方法“passwordVerification()”将始终返回null。

Answer 1:

当从单个文件导出多个模型,如你在schema.js,你需要给每个导出模型自身的exports字段名。

例如,更换多个module.exports = ...与在该出口的所有型号的文件结束此行代码在schema.js:

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

然后在index.js您可以访问像这样的机型:

var models = require('./schema');
...
models.User.findOne(...


文章来源: cant get data from database after multiple schema declared (mongoose + express + mongodb