[英]Uncaught TypeError: Cannot read properties of undefined: React Typescript
I'm working on fetching data with React and graphql and render it with react/pdf-renderer.我正在使用 React 和 graphql 获取数据并使用 react/pdf-renderer 渲染它。
I use renderContent but it gives me this error.我使用 renderContent 但它给了我这个错误。 What I'm doing wrong?
我做错了什么?
Uncaught TypeError: Cannot read properties of undefined (reading 'bookingConfirmationNumber')at renderContent
未捕获的类型错误:无法在渲染内容中读取未定义的属性(读取“bookingConfirmationNumber”)
Below is my code下面是我的代码
const renderContent = () => {
const hotelDetails: HotelDetailsType = data.getHotelDetailsByIds.items[0]
return (
<PDFViewer>
<Document>
console.log(hotelDetails.bookingConfirmationNumber)
<Page>
<View>
<Text>{hotelDetails.bookingConfirmationNumber}</Text>
<Text>{hotelDetails.hotelName}</Text>
<Text>{hotelDetails.address}</Text>
<Text>{hotelDetails.hotelContactNumber}</Text>
<Text>{hotelDetails.guestName}</Text>
<Text>{hotelDetails.guests.adults}</Text>
<Text>{hotelDetails.guests.children}</Text>
</View>
</Page>
</Document>
</PDFViewer>
)}
Here is my schema这是我的架构
export type HotelDetailsType = {
bookingConfirmationNumber: string
hotelName: string
address: string
hotelContactNumber: string
guestName: string
guests: HotelGuestsType
}
I tried optional chaining, it renders the PDF component but data were not being fetched and Query is working fine in Graphql.我尝试了可选链接,它呈现了 PDF 组件,但没有获取数据,并且查询在 Graphql 中工作正常。 Any inputs and resource to read on is highly appreciated.
非常感谢您阅读的任何输入和资源。
Thank you.谢谢你。
hotelDetails
is resulting in undefined simply because data.getHotelDetailsByIds.items[0]
(and data.getHotelDetailsByIds
in the previous version of your question) is undefined. hotelDetails
导致 undefined 仅仅是因为data.getHotelDetailsByIds.items[0]
(以及您问题的先前版本中的data.getHotelDetailsByIds
)未定义。 When you inspect data
, you should see the issue.当您检查
data
时,您应该会看到问题。
It looks like data.getHotelDetailsByIds
returns empty array.看起来
data.getHotelDetailsByIds
返回空数组。 So, data.getHotelDetailsByIds.item[0]
is undefined
.因此,
data.getHotelDetailsByIds.item[0]
是undefined
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.