【发布时间】:2019-11-29 23:32:09
【问题描述】:
我正在 Next/React.JS 中开发视频/音乐流应用程序,从 Wordpress API/Backend/headless CMS 获取数据。它在 localhost 上运行良好(尽管目前它是真正的准系统功能) - 但是,当我尝试导出它以创建静态前端时,导出反复失败,并出现(可笑的常见且通常很简单)'无法读取未定义的属性' 错误。
在过去的 12 个小时里,我一直在严格调试并扫描 here/GH 等,在阳光下使用 then()/catch() await 等的所有组合,但我就是无法让它工作并且可以' t 为我的生活找出原因。我知道“标题”是未定义的,因为在导出时尚未获取数据,但是如何获取过去的“下一个导出”? 这是我的来自 single.js 的 getInitialProps - 问题似乎出在哪里,在控制台中没有其他相关错误 - 我将在下面尝试导出时发布终端消息。这是我在几十个版本之后回到的地方 - 这是漫长的一天,所以可能有一两个愚蠢的错误,但这个 在本地运行, 没有任何错误。
static async getInitialProps(context) {
const slug = context.query.slug;
let post = {};
// Make request for posts.
try {
const response = await axios.get(
`http://timeline-music-30.local/wp-json/wp/v2/posts?slug=${slug}`
);
post = response.data[0];
} catch (err) {
console.error(err);
}
return { post };
// Return our only item in array from response to posts object in props.
console.log("post:", post);
}
我希望应用程序成功导出到静态站点,但它失败并显示以下终端消息:
copying "static build" directory
launching 3 threads with concurrency of 10 per thread
[==--] 2/4 50% 118/s 0.0s TypeError: Cannot read property 'title' of undefined
at _default.render (C:\Users\Terry\Documents\github\projects\timeline-music-3.0\nextjs\.next\server\static\YyI9s0TjENSVhc1SFZUcV\pages\single.js:158:35)
任何想法/帮助将不胜感激。
谢谢
特里
【问题讨论】:
标签: javascript reactjs axios next.js