当前位置: 首页 > 知识库问答 >
问题:

声明多个架构后无法从数据库获取数据(mongoose express mongodb

燕璞
2023-03-14

我是新来的。当声明多个mongoose模式时,js和我在访问时遇到问题。

//模式。模型中的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;

//索引。控制器中的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'});
    }
});
}

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

共有1个答案

轩辕阳焱
2023-03-14

当从单个文件导出多个模型时,如在schema.js中,您需要为每个导出的模型提供自己的导出字段名。

例如,将schema.js中的多行module.exports=...替换为导出所有模型的文件末尾的代码:

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

然后在索引中。js您可以访问以下模型:

var models = require('./schema');
...
models.User.findOne(...
 类似资料:
  • 我正在尝试获取我的用户名,并在应用程序打开时向他/她问好。但我无法根据某个用户的uid获取数据。当我运行应用程序时,祝酒词从未真正出现。 数据库结构 密码

  • 我有一个问题,从多个路径从Firebas eDatabase获取数据。我正在编写一个Endomondo克隆,我有一个数据库结构如下: 现在我想在feed选项卡中列出我朋友的所有锻炼。所以要做到这一点,我需要做三件事。 1)遍历我的配置文件(user_1)以获取我所有朋友的ID。 2)当我拥有这些ID时,我需要迭代它们的配置文件以获得它们的名称来显示。 3)遍历锻炼以获得我朋友的所有锻炼。 现在我想

  • 本文向大家介绍node.js从数据库获取数据,包括了node.js从数据库获取数据的使用技巧和注意事项,需要的朋友参考一下 本文需要用node.js做一个从Sqlserver获取数据并显示到页面上的小功能,下面就为大家分享: app.js: 接下来就直接在页面中使用get方式请求即可,当然post方式也是类似原理。 还有我发现textarea控件在改变其text和html属性的时候,value还保

  • 问题内容: 我最近将应用程序中的hiberbnate模式更新为5.4.4.Final。现在,在部署过程中我面临以下异常。 我使用以下persistence.xml。 经过进一步调查,我发现根本原因是:hibernate使用 SequenceInformation 接口进行序列元数据操作 但是,我的应用程序使用如下序列: 该 Long.MAX_VALUE 等于9223372036854775807,

  • 问题内容: 我试图找到一种方法来查找数据库中的表的名称(如果存在)。我发现从sqlite cli我可以使用: 然后对于字段: 这显然在python中不起作用。有没有办法用python做到这一点,还是我应该只使用sqlite命令行? 问题答案: 您应该能够从表中访问表名称。 列名不能直接访问。获取它们的最简单方法是查询表并从查询结果中获取列名。

  • 问题内容: 我在使用JPA / Spring遇到一个特定问题时遇到了一些麻烦: 如何为实体动态分配架构? 我们有属于模式AD的TABLE1和位于BD下的TABLE2。 架构可能不会在注释属性中进行硬编码,因为它取决于环境(Dev / Acc / Prd)。(接受的模式是S1A和S2A) 我该如何实现?是否可以指定这样的占位符: 以便根据驻留在环境中的属性文件替换架构? 干杯 问题答案: 我有一个相