Tôi mới đến Node.js và tôi đang gặp vấn đề truy cập vào khi schema nhiều cầy mangut đã khai báo.không thể lấy dữ liệu từ cơ sở dữ liệu sau nhiều lần tuyên bố schema (mongoose + hiện + MongoDB
//schema.js trong mô hình
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 trong điều khiển
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'});
}
});
}
Khi tôi chỉ có "giản đồ người dùng" trong schema.js của tôi, cuộc gọi cơ sở dữ liệu từ phương pháp "passwordVerification()" từ chỉ số .js sẽ trả lại urn tôi mật khẩu có liên quan đã được lấy từ cơ sở dữ liệu. Tuy nhiên, khi tôi bắt đầu thêm vào lược đồ khác chẳng hạn như "Question Schema" trong schema.js, phương thức "passwordVerification()" sẽ luôn trả về null.
khi tôi chỉ có khai báo userSchema, tôi có thể truy xuất dữ liệu từ bảng người dùng. Tuy nhiên, khi tôi thêm vào qnSchema, tôi không thể truy xuất dữ liệu từ bảng người dùng và nó chỉ trả về null –
Tôi đã chỉnh sửa câu hỏi. Tôi hy vọng bạn sẽ có thể hiểu được vấn đề. Xin lỗi vì tiếng anh xấu –
Bạn tiếp tục ghi đè 'exports.module' trong schema.js và index.js không phải là schema.js' require'. Đây có phải là mã thực tế bạn đang chạy không? – JohnnyHK