【发布时间】:2020-08-04 19:43:31
【问题描述】:
只是想知道处理空数据的最佳方法是什么,在此处作为变量传递给 {useQuery}。目前,每当 categoryId 为空时,我都会收到错误“TypeError:无法读取未定义的属性‘节点’”。我没有包含我的 graphQL 查询,因为它可能没有必要。提前致谢。
const RelatedContent = ({categories}) => {
const {data, loading, error} = useQuery(GET_TITLES_BY_CATEGORY, {
variables: {
categoryId: categories.edges[0].node.databaseId
},
fetchPolicy: 'no-cache'
})
if (loading) return <div>Loading...</div>
if (error) return `Error! ${error.message}`
return (
<div className={styles.sectionBackground + " section"}>
<div className="container">
<h3 className={styles.title}>Related film and TV</h3>
<div className="columns is-multiline is-mobile">
{
data.impactArticles.edges.map((item) => {
return (
<div className="column">
<ContentCard
item={item}
/>
</div>
)
})
}
</div>
</div>
</div>
)
}
【问题讨论】:
标签: javascript reactjs graphql apollo