【问题标题】:passing data to partial express js hbs将数据传递给部分快递 js hbs
【发布时间】:2021-07-30 20:26:45
【问题描述】:

目前我正在使用 node/express/hbs 和 mysql 制作通知系统。我可以正确地向数据库插入通知,但我无法获取我的部分文件。

这是我的代码

  countNotification: async (req, res) => {
            await Notification.count({ where: { status: 'unread' } }).then((notification) => {
                if (notification) {
                    const dashNotification = req.notification;
                    return dashNotification;
                }
            });
        },



 app.use((req, res, next) => {
        res.locals.user = req.user;
        res.locals.notification = req.dashNotification;
        next();
    });

我试过这样,但它不起作用,有人有任何解决方案吗?

【问题讨论】:

    标签: node.js express session notifications


    【解决方案1】:

    您应该使用路由器上方的中间件:

         app.use((req, res, next) => {
                res.locals.user = req.user;
                res.locals.notification = req.dashNotification;
                next();
            });
          
        countNotification: async (req, res) => {
                    await Notification.count({ where: { status: 'unread' } }).then((notification) => {
                        if (notification) {
                            const dashNotification = req.notification;
                            return dashNotification;
                        }
                    });
                },
    
    

    【讨论】:

    • 感谢@CoderX701
    猜你喜欢
    • 2013-11-17
    • 1970-01-01
    • 2013-11-30
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2018-01-25
    相关资源
    最近更新 更多