reactjs - 仅在显示道具时出现 Vercel 部署错误(在本地工作)
问题描述
我正在构建一个无头 wordpress CMS(并使用 next.js 模板)。
我能够成功部署到vercel服务器,然后开始添加一些内容,在这种情况下是Wordpress标题字段..这在我的本地服务器上效果很好,并且可以正确显示标题而没有错误..但是当我更新时我的 git 它杀死了 vercel 部署,我收到了这个错误。如果我尝试使用内容或危险的 SetHTML,我也会收到这个错误
澄清一下,如果我删除它,它会很好地部署 { page.title }
Unhandled error during request: TypeError: Cannot read property 'title' of undefined
--
17:01:15.529 | at Page (/vercel/workpath0/.next/serverless/pages/[slug].js:1634:412)
这是我的页面代码 [slug].js
import Head from 'next/head'
import Link from 'next/link'
import Container from '../components/container'
import MoreStories from '../components/more-stories'
import HeroPost from '../components/hero-post'
import Intro from '../components/intro'
import Layout from '../components/layout'
import { getAllPagesWithSlug, getAllPagesBySlug } from '../lib/api'
import { CMS_NAME } from '../lib/constants'
import Header from '../components/header'
export default function Page( {page} ) {
return (
<>
<Layout>
<Head>
<title></title>
</Head>
<Header />
<Container>
{ page.title }
</Container>
</Layout>
</>
)
}
export async function getStaticProps({ params }) {
const data = await getAllPagesBySlug(params.slug)
console.log(data)
return {
props: {
page: data.page,
},
}
}
export async function getStaticPaths() {
const allPages = await getAllPagesWithSlug()
return {
//paths: [ { params: { slug: '${node.uri}' } } ],
paths: allPages.edges.map(({ node }) => `/${node.slug}`) || [],
fallback: true,
}
}
这是我的查询 lib/api
const API_URL = process.env.WORDPRESS_API_URL
async function fetchAPI(query, { variables } = {}) {
const headers = { 'Content-Type': 'application/json' }
if (process.env.WORDPRESS_AUTH_REFRESH_TOKEN) {
headers[
'Authorization'
] = `Bearer ${process.env.WORDPRESS_AUTH_REFRESH_TOKEN}`
}
const res = await fetch(API_URL, {
method: 'POST',
headers,
body: JSON.stringify({
query,
variables,
}),
})
const json = await res.json()
if (json.errors) {
console.error(json.errors)
throw new Error('Failed to fetch API')
}
return json.data
}
export async function getAllPagesBySlug($id) {
const data = await fetchAPI(`
{
page(id: "${$id}", idType: URI) {
uri
slug
content
title
featuredImage {
node {
sourceUrl
}
}
}
}
`)
return data
}
解决方案
推荐阅读
- php - PHPMailer不发送电子邮件(如果构造)
- python - tkinter Radiobutton:如何将多行文本与按钮对齐?
- neo4j - 使用 C#neo4j 客户端将关系属性添加到返回节点?
- scala - 如何执行单个嵌套的 ScalaTest Suite?
- python - 获取列表中可用的免费余额
- kubernetes - 在 kustomization.yaml 中排除资源
- twitter - 等待 Twitter 的 API 限制重置后再继续
- javascript - 如何计算输入栏的内容?- 反应JS
- vue.js - vue 3 使用 Vuex 和路由器的服务器端渲染
- java - 使用java从Array中过滤id值