2016-07-02 57 views
0

我有一個小問題,就是在我的mongo/mongoose數據庫中顯示用戶名在HTML頁面中。到目前爲止,我已經編寫了這個代碼,它在我的控制檯中打印出數據庫中的所有用戶,並且它工作得很好。顯示MongoDB/Mongoose/Handlebars的用戶名

User.find(function (err, users, res) { 
    if (err) return console.error(err); 
    console.log(users); 
}); 

但我希望能夠在我的HTML頁面上打印一個整潔的小表中的用戶名。這樣的事情:

<table class="table"> 
    <tr> 
     <td> 
      {{I want my users to be displayed here in a list.}} 
     </td> 
    </tr> 
</table> 

我使用把手作爲我的視圖引擎。任何幫助解決這個問題將不勝感激,因爲我一直堅持這一天了一天。如果你需要我的用戶模式或其他任何東西,只要告訴我,我會發布它。由於

index.js

var express = require('express'); 
var router = express.Router(); 
var app = express(); 


// Get Homepage 
router.get('/', ensureAuthentication, function (req, res) { 
    res.render('index'); 
}); 

// Get Assignments 
router.get('/assignments', ensureAuthentication, function (req, res) { 
    res.render('assignments'); 
}); 

// Get userlist 
router.get('/userslist', ensureAuthentication, function (req, res) { 
    res.render('userslist'); 
}); 


function ensureAuthentication(req, res, next) { 
    if (req.isAuthenticated()) { 
     return next(); 
    } else { 
     res.redirect('/users/login'); 
    } 
} 

module.exports = router; 

users.js

var express = require('express'); 
var router = express.Router(); 
var passport = require('passport'); 
var LocalStrategy = require('passport-local').Strategy; 

var User = require('../models/user'); 

// Register 
router.get('/register', function (req, res) { 
    res.render('register'); 
}); 

// Login 
router.get('/login', function (req, res) { 
    res.render('login'); 
}); 

// Register User 
router.post('/register', function (req, res) { 

    var name = req.body.name; 
    var email = req.body.email; 
    var username = req.body.username; 
    var password = req.body.password; 
    var password2 = req.body.password2; 

    // Validation 
    req.checkBody('name', 'Name is required').notEmpty(); 
    req.checkBody('email', 'Email is required').notEmpty(); 
    req.checkBody('email', 'Email is not valid').isEmail(); 
    req.checkBody('username', 'Username is required').notEmpty(); 
    req.checkBody('password', 'Password is required').notEmpty(); 
    req.checkBody('password2', 'Passwords do not match').equals(req.body.password); 

    var errors = req.validationErrors(); 

    if (errors) { 
     res.render('register', { 
      errors: errors 
     }); 
    } else { 
     var newUser = new User({ 
      name: name, 
      email: email, 
      username: username, 
      password: password 
     }); 

     User.createUser(newUser, function (err, user) { 
      if (err) throw err; 
      console.log(user); 
     }); 

     req.flash('success_msg', 'You are now registered. Redirecting to login page.'); 

     res.redirect('/users/login'); 

    } 
}); 

passport.use(new LocalStrategy(
    function (username, password, done) { 
     User.getUserByUsername(username, function (err, user) { 
      if (err) throw err; 
      if (!user) { 
       return done(null, false, { message: 'Unknown User' }); 
      } 

      User.comparePassword(password, user.password, function (err, isMatch) { 
       if (err) throw err; 
       if (isMatch) { 
        return done(null, user); 
       } else { 
        return done(null, false, { message: 'Invalid password' }); 
       } 
      }); 
     }); 
    })); 

passport.serializeUser(function (user, done) { 
    done(null, user.id); 
}); 

passport.deserializeUser(function (id, done) { 
    User.getUserById(id, function (err, user) { 
     done(err, user); 
    }); 
}); 

router.post('/login', 
    passport.authenticate('local', { successRedirect: '/', failureRedirect: '/users/login', failureFlash: true }), 
    function (req, res) { 
     res.redirect('/'); 
    }); 

router.get('/logout', function (req, res) { 
    req.logout(); 

    req.flash('success_msg', 'Logged out successfully.'); 

    res.redirect('/users/login'); 
}); 


// Get users 
User.find(function (err, users, res) { 
    if (err) return console.error(err); 
    res.render('users', { userList: users }); 
    //console.log(users); 
}); 


module.exports = router; 

回答

1

使用res.render()從回調中呈現你的看法:

router.get('/overview', function(req, res) { 
    User.find(function (err, users, res) { 
    if (err) return res.sendStatus(500); 
    res.render('users', { userList : users }); 
    }); 
}); 

然後,在你的模板,你呈現用戶列表(作爲userList通過傳遞3210調用):

<td> 
    <ul> 
    {{#each userList}} 
     <li>{{name}}</li> 
    {{/each}} 
    </ul> 
</td> 

name應該在你的架構中的屬性名稱包含用戶名)

+0

謝謝你的回答,但是我得到類型錯誤:無法讀取屬性「渲染」的未定義。我在嘗試使用render()時遇到過這個問題,但我不知道如何解決這個問題:( –

+0

你可以在你的問題中添加你的Express路由代碼(應該呈現視圖)嗎? – robertklep

+0

Added index.js和users.js,這是我目前在我的API中使用的兩條路線 –