javascript - getStaticPaths() 返回 404 Page Not Found
问题描述
getStaticPaths()
不断抛出 404 页面,我猜这是因为该函数旨在getAllPostIds()
返回一个对象数组。我正在使用打字稿,我已经声明了对象数组,但我不确定如何在id
interface ArrObj {
params: {
id: string;
}
}
let arrObj: ArrObj[];
export function getAllPostIds() {
const fileNames = fs.readdirSync(postsDirectory);
return fileNames.map(fileName => {
return {
params: {
id: fileName.replace(/\.md$/, '')
}
}
})
}
export function getPostData(id) {
const fullPath = path.join(postsDirectory, `${id}.md`)
const fileContents = fs.readFileSync(fullPath, 'utf8')
const matterResult = matter(fileContents)
return {
id,
...matterResult.data
}
}
export async function getStaticPaths(){
const paths = getAllPostIds();
return{
paths,
fallback: false
}
}
export async function getStaticProps({ params }) {
const postData = getPostData(params.id)
return {
props: {
postData
}
}
}
解决方案
推荐阅读
- java - Eclips-pmd 要求 Eclipse 在 JVM 版本 11 或更高版本上运行
- flask - Flask-socketio 断开客户端
- java - 重新排列字符串的字符
- asp.net-mvc - 如何在MVC的Highcharts中动态添加分组的_categories
- javascript - 使用 HERE API 3/3.1 移动可拖动标记时,如何重新计算路线?
- reactjs - Antd:如何更改 Antd Form.Item 标签文本颜色?
- swift - Swift:为函数类型专门化泛型类的方法
- javascript - 无法访问 api 对象属性 - 反应钩子
- c - 当我将整数复制到新的动态数组中时,为什么会得到奇怪的值?
- c# - 计时器结束前游戏对象未停用