【问题标题】:Cannot display value from Mongodb on my Jade page无法在我的 Jade 页面上显示来自 Mongodb 的值
【发布时间】:2016-01-23 22:38:10
【问题描述】:

我有以下路线:

app.get('/dashboard', passportConf.isAuthenticated, userController.getBoardsCount,userController.getSharedBoardsCount, userController.getBoards);

这是我用来从 MongoDB 获取值的中间件代码:

exports.getBoardsCount = function(req, res, next) {
    var count = 0;
    Room.count({
        "owner": req.user._id
    }, function(err, count) {
        if (err) {
            console.log(err);
        }
        if (count) {
            req.count = count;
        }
        next();
        console.log(count);
    })
};

exports.getSharedBoardsCount = function(req, res, next) {
    var sharedBoards = 0;
    Room.count({
        $and: [{"owner": {$ne: req.user._id}},{ users : req.user._id}]
    }, function(err, sharedBoards) {
        if (err) {
            console.log(err);
        }
        if (sharedBoards) {
            req.sharedBoards = sharedBoards;
        }
        next();
        console.log(sharedBoards);
    })
};

我可以使用#{count} 在我的jade 文件上显示count,但是sharedBoards 没有出现,尽管它正在显示在终端中。我使用#{sharedBoards} 在我的 Jade 文件中显示它。你们能帮我解决这个问题吗?我似乎找不到问题所在。

【问题讨论】:

  • 看起来您的路线在passportConf.isAuthenticated 之后缺少逗号?
  • 我刚查了一下,那里有个逗号。我在这里写的时候错过了。编辑我已经用逗号更新了上面的 sn-p。
  • 没人帮我吗?我敢肯定这一定很傻
  • Jade 文件的渲染代码在哪里?
  • 渲染代码是什么意思?抱歉,我对这个堆栈很陌生。

标签: node.js mongodb express pug


【解决方案1】:
exports.getBoards = function(req, res) {

Room.find({
    users: req.user._id
}, function(
    err,
    rooms) {
    if (err) {
        return err;
    }


        res.render('account/dashboard', {
            rooms: rooms,
                            count:req.count,
                            sharedBoards:req.sharedBoards
        });

});

 };

我拼错了 sharedBoards。

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 2021-11-30
    • 2023-03-24
    • 2016-08-04
    • 2012-12-28
    相关资源
    最近更新 更多