从nodejs中的mongodb获取数据

时间:2015-07-27 15:56:06

标签: node.js mongodb

我使用以下代码从mongodb获取数据

但是.jade文件

中未定义用户列表
var db = monk('mongodb://UserName:Password@ds047342.mongolab.com:ds047342/UsersDB');



router.get('/userlist', function (req, res) {
    var db = req.db;
    var collection = db.get('Users');

    collection.find({}, {}, function (e, docs) {
        res.render('userlist', {
            "userlist": docs
        });
    });
});

jade文件中的代码

extends layout

    block content
        h1.
            User List
        ul
            each user, i in userlist
                li
                    a(href="mailto:#{user.EmailId}")= "name"

1 个答案:

答案 0 :(得分:0)

我无法复制您的问题并成功从本地收藏中加载用户,也许您需要在呈现页面之前进行验证

router.get('/userlist', function (req, res) {
    var db = req.db;
    var collection = db.get('Users');

    collection.find({}, {}, function (e, docs) {
        if (e){
            // something went wrong, render an error
        } else if (!docs) {
            // no users found, render a 404 maybe
        } else {
            // users found, render the list
            res.render('userlist', {
                "userlist": docs
            });
        }
    });
});