首页 > 解决方案 > 无法将 undefined 或 null 转换为对象:Next.js || 反应

问题描述

当前收到错误说明Cannot convert undefined or null to object

应用程序正在寻找的数据来自初始道具。我假设在初始检查中不存在任何数据,因此会引发该错误。

这可以用一个来解决async/await吗?

初始 posts_mentions 默认为空对象

这是错误图像: 在此处输入图像描述

这是当前的代码片段

const { posts_mentions: postsMentions } = useData();
    const data = Object.keys(postsMentions).map(label => {
        return {
            name: shortName(label),
            posts: postsMentions[label].posts,
            mentions: postsMentions[label].mentions
        }
    })

标签: arraysreactjsobjectnext.js

解决方案


async function something(){
const { posts_mentions: postsMentions } = await useData();
 const data = Object.keys(postsMentions).map(label => {
    return {
        name: shortName(label),
        posts: postsMentions[label].posts,
        mentions: postsMentions[label].mentions
    }
})

}


推荐阅读