Node.js中使用async/await时返回对象正常但取值为undefined的问题排查
问题排查与修复方案
嘿,我帮你梳理下代码里的问题,你遇到的undefined主要来自两个核心原因:
核心问题分析
1. 错误处理缺失导致函数返回undefined
你的notification异步函数里,当数据库查询出错时,catch块仅打印了错误,但没有重新抛出错误或返回默认值。异步函数在出错且无返回值时,会默认返回undefined,这就导致notificationInfo变成undefined,后续访问它的status自然会报错。
2. 混淆了Sequelize方法的返回值
Notification.findAll()返回的是符合条件的模型实例数组,不是单个对象!就算查询成功,notificationInfo是一个数组,数组本身根本没有status属性,所以你写notificationInfo.status肯定得到undefined。如果你的需求是获取单条未读通知,应该用findOne而非findAll。
修复方案
情况一:需要获取所有未读通知(数组)
如果确实要拿全部未读通知,得遍历数组访问每个元素的status,同时完善错误处理:
const Notification = require('../../../models/Notification'); module.exports = function onRouterload() { return { allRouter: async (req, res, next) => { // 设置admin布局 req.app.locals.layout = 'admin'; try { // 直接await查询,去掉冗余的嵌套函数 const notificationInfo = await Notification.findAll({ where: { status: 'unread' } }); // 访问单个通知的status需要遍历数组,比如打印第一个的状态 if (notificationInfo.length > 0) { console.log(notificationInfo[0].status); } else { console.log("暂无未读通知"); } res.locals.notification = notificationInfo; next(); } catch (error) { console.error("获取通知失败:", error); // 将错误传递给后续错误处理中间件,避免静默失败 next(error); } }, }; };
情况二:只需要单个未读通知
如果你的需求是获取一条未读通知,把findAll换成findOne即可,它返回单个对象(或null):
const Notification = require('../../../models/Notification'); module.exports = function onRouterload() { return { allRouter: async (req, res, next) => { req.app.locals.layout = 'admin'; try { const notificationInfo = await Notification.findOne({ where: { status: 'unread' } }); // 现在可直接访问status,记得先判断对象是否存在 if (notificationInfo) { console.log(notificationInfo.status); } else { console.log("暂无未读通知"); } res.locals.notification = notificationInfo; next(); } catch (error) { console.error("获取通知失败:", error); next(error); } }, }; };
额外小优化
没必要在中间件里嵌套另一个异步函数,直接把查询逻辑写在allRouter里,代码会更简洁易读~
内容的提问来源于stack exchange,提问作者freelanceing mindset
相关产品推荐
相关产品推荐

