将数据传递给 partial express js hbs

passing data to partial express js hbs

目前我正在用 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();
    });

我已经试过了,但是没有用,有人有解决办法吗?

你应该在路由器之上使用你的中间件:

     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;
                    }
                });
            },