我正在使用next.js和wordpress。我已经设置了api.js。每当我尝试[slug]
从另一个组件访问时,都会显示- Typerror: Cannot read property 'slug' of undefined
。
假设,Test
如果我尝试通过具有下一个链接的组件访问帖子,则还有一个名为的页面。
<Link href={`/articles/${node.slug}`}>
<a><h5>{post.name}</h5></a>
</Link>
它显示了 Typerror: Cannot read property 'slug' of undefined
我的代码可以在创建该组件的相同组件中正常工作,但是每当我尝试使用下一个链接从另一个组件访问它时,我的代码都会正常工作。它显示错误。
我希望它是很好的理解。
这是我的代码:
api.js:
const API_URL = process.env.WP_API_URL;
async function fetchAPI(query, { variables } = {}) {
const headers = { 'Content-Type': 'application/json' };
const res = await fetch(API_URL, {
method: 'POST',
headers,
body: JSON.stringify({ query, variables })
});
// error handling work
const json = await res.json();
if (json.errors) {
console.log(json.errors);
console.log('error details', query, variables);
throw new Error('Failed to fetch API');
}
return json.data;
}
export async function getAllPosts(preview) {
const data = await fetchAPI(
`
query AllPosts {
posts(first: 20, where: {orderby: {field: DATE, order: DESC}}) {
edges {
node {
id
date
title
slug
}
}
}
}
`
);
return data?.posts;
}
export async function getAllPostsWithSlug() {
const data = await fetchAPI(
`
{
posts(first: 10000) {
edges {
node {
slug
}
}
}
}
`);
return data?.posts;
};
export async function getPost(slug) {
const data = await fetchAPI(
`
fragment PostFields on Post {
title
excerpt
slug
date
featuredImage {
node {
sourceUrl
}
}
}
query PostBySlug($id: ID!, $idType: PostIdType!) {
post(id: $id, idType: $idType) {
...PostFields
content
}
}
`,
{
variables: {
id: slug,
idType: 'SLUG'
}
}
);
return data;
};
[slug] .js:
<article>
<h1>{postData.title}</h1>
<div dangerouslySetInnerHTML={{ __html: postData.content }} />
</article>
export async function getStaticPaths() {
const allPosts = await getAllPostsWithSlug();
return {
paths: allPosts.edges.map(({ node }) => `/articles/${node.slug}`) || [],
fallback: true
};
}
export async function getStaticProps({ params }) {
const data = await getPost(params.slug);
return {
props: {
postData: data.post
}
};
}
GraphQL查询
query AllProfiles {
businessProfiles(where: {orderby: {field: AUTHOR, order: ASC}}) {
edges {
node {
date
title
slug
link
uri
}
}
注意:由于stackoverflow验证,我缩短了代码。现在,问题出在每当我尝试从另一个组件访问路径时,它都会显示我上面提到的错误。
我正在使用Next链接进行路由和所有操作。
反正要解决吗?请指导我。
谢谢你们。
从上面使用graphql查询
query AllProfiles {
businessProfiles(where: {orderby: {field: AUTHOR, order: ASC}}) {
edges {
node {
date
title
slug
link
uri
}
}
}
}
假设您将查询定位为子弹,Link
并且您的函数如下所示
export async function getAllProfiles() {
const data = await fetchAPI(`
query AllProfiles {
businessProfiles(where: {orderby: {field: AUTHOR, order: ASC}}) {
edges {
node {
date
title
slug
link
uri
}
}
}
}
`);
return data?.posts;
}
然后,您将希望隔离段塞。怎么样?制作一个独立的getProfileSlug组件。
import { getAllProfiles } from '@lib/api';
import Link from 'next/link';
const resolveProfileSlugs = async ({ ...props }) => {
const data = await getAllProfiles();
return data !== null &&
data.posts !== null &&
data.posts.edges !== null &&
data.posts.edges.length > 0 ? (
data.posts.edges.map(slugs => {
return slugs !== null &&
slugs.node !== null &&
slugs.node.slug !== null &&
slugs.node.title !== null ? (
<>
<Link href={`/articles/${slugs.node.slug}`} passHref key={slugs.node.id}>
<a {...props}>
{slugs.node.title}
</a>
</Link>
</>
) : (
<div>{'Slug or Title Null'}</div>
)
})
) : (
<div>
{
'data from getAllProfiles either null, undefined, and/or has an edges.length = 0'
}
</div>
);
};
export default resolveProfileSlugs;
我对所有wpgraphql项目都使用next / typescript。随意浏览我最近的回购-https: //github.com/DopamineDriven/drisdell-consulting-services
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句