如果我想要达到的目标是正确的,那么不是100%确定,所以如果我错了请纠正我。
我正在构建一个nodejs,express&护照网站/ app。
在我的routes.js中,我有以下部分:
/* GET Home Page */
router.get('/dashboard', isAuthenticated, function(req, res){
res.render('dashboard', {
user: req.user,
sess: req.session
});
});
用户登录后,会显示“仪表板”。在那个“仪表板”上,我想包括他们最近的10个日志条目。我设置了logbook.js
模型,我只是不确定如何调用它。
models/logbook.js
中的我的功能是:
function getLatestEntries(req, res, user){
Logbook.find({ 'uid' : user.uid }, {}, { limit: 10}, function(err, logbook){
return logbook;
});
}
logbook.js的内容:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var logbookSchema = new Schema({
id: String,
uid: String,
lid: { type: String, unique: true},
callsign: String,
contact: String,
start: String,
end: String,
band: String,
mode: String,
RSTsent: String,
RSTrec: String,
notes: String
});
var Logbook = mongoose.model('Logbook', logbookSchema);
function getLatestEntries(req, next){
Logbook.find({ 'uid' : sess.uid }, {}, { limit: 10}, function(err, logbook){
if (err){
console.log("Error"+err)
return next(err, null);
}
return next(null, logbook);
});
}
module.exports.getLatestEntries = getLatestEntries;
// make this available to our users in our Node applications
module.exports = Logbook;
答案 0 :(得分:1)
在你的routes.js中:
var Logbook = require('./models/logbook'); // change path if it's wrong
/* GET Home Page */
router.get('/dashboard', isAuthenticated, function(req, res, next) {
Logbook.find({ 'uid' : req.user.uid }, {}, { limit: 10}, function(err, logbook){
if (err) { console.log(err); return next(); }
res.render('dashboard', {
user: req.user,
logbook: logbook
});
});
});
在你的models / logbook.js中:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var logbookSchema = new Schema({
id: String,
uid: String,
lid: { type: String, unique: true},
callsign: String,
contact: String,
start: String,
end: String,
band: String,
mode: String,
RSTsent: String,
RSTrec: String,
notes: String
});
var Logbook = mongoose.model('Logbook', logbookSchema);
// make this available to our users in our Node applications
module.exports = Logbook;