如何從內部路線呼叫模型?

[英]How to call model from inside route?


Not 100% certain if what I want to achieve is the correct way of doing it, so please correct me if I'm wrong.

不是100%肯定,如果我想要實現的是正確的方法,所以如果我錯了請糾正我。

I'm building a nodejs, express & passport website/app.

我正在建立一個nodejs,express和passport website / app。

In my routes.js, I have the following section:

在我的routes.js中,我有以下部分:

/* GET Home Page */
router.get('/dashboard', isAuthenticated, function(req, res){
    res.render('dashboard', { 
        user: req.user,
        sess: req.session
    });
});

After the user is logged in, it displays the 'dashboard'. On that 'dashboard' I'd like to include 10 of their recent logbook entries. I have logbook.js model set up, I'm just unsure how to call it.

用戶登錄后,將顯示“儀表板”。在那個“儀表板”上,我想包括他們最近的10個日志條目。我有logbook.js模型設置,我只是不確定如何調用它。

My function within models/logbook.js is:

我在models / logbook.js中的功能是:

function getLatestEntries(req, res, user){
Logbook.find({ 'uid' :  user.uid }, {}, { limit: 10}, function(err, logbook){
    return logbook;
});
}

Contents of logbook.js:

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;

1 个解决方案

#1


In your routes.js:

在您的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
        });
    });
});

In your models/logbook.js:

在你的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;

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/04/73008059b514994ee5477e7b05fc022e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com