arrays - 无法将 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
}
})
解决方案
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
}
})
}
推荐阅读
- java - javax.websocket.DeploymentException:在尝试部署到 Tomcat 时,未找到方法 [OnMessage] 上存在的消息参数的解码器
- ios - 通过 Swift Audio Player 在 iOS swift 中播放 .mp3 中的实时音频流
- typescript - 如何在javascript中使用来自其他常量的变量?
- user-interface - 即使所有服务都在运行并且 9870 端口正在侦听,也无法访问 Hadoop Web 界面
- macos - 无法与非 AWS 服务通话
- sql - 如何使用连接 ID 在 SQL Server 的循环中将表插入 N 次?
- javascript - Javascript / jQuery HTML5 视频 - 使用监听器捕获 net::ERR_SSL_PROTOCOL_ERROR
- node.js - 处理构建到与源不同目录中的节点服务器的相对路径
- python-3.x - Bitbucket:ModuleNotFoundError:没有名为“numpy”的模块
- mysql - 对于这个复杂的数据,查询应该是什么样子?