简体   繁体   English

未捕获的类型错误:无法读取未定义的属性:反应 Typescript

[英]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.

相关问题 React - 未捕获的类型错误:无法读取未定义的属性 - React - Uncaught TypeError: Cannot read properties of undefined 反应:未捕获的类型错误:无法读取未定义的属性 - React: Uncaught TypeError: Cannot read properties of undefined 未捕获的类型错误:反应原生中的“无法读取未定义的属性” - Uncaught TypeError: "Cannot read properties of undefined" in react native 反应,Redux:未捕获的类型错误:无法读取未定义的属性(读取“区域”) - React, Redux: Uncaught TypeError: Cannot read properties of undefined (reading 'region') react JS错误:未捕获(承诺中)TypeError:无法读取未定义的属性 - react JS error: Uncaught (in promise) TypeError: Cannot read properties of undefined React Js - 未捕获的类型错误:无法读取未定义的属性 - React Js - Uncaught TypeError: Cannot read properties of undefined 未捕获的类型错误:无法读取未定义的属性(读取“地图”)反应 - Uncaught TypeError: Cannot read properties of undefined (reading 'map') React 反应:未捕获的类型错误:无法读取未定义的属性(读取“charAt”) - React: Uncaught TypeError: Cannot read properties of undefined (reading 'charAt') 未捕获的 TypeError:无法读取未定义的属性(读取“params”)-React - Uncaught TypeError: Cannot read properties of undefined (reading 'params') - React 未捕获的 TypeError:无法读取 Simple React 项目中未定义(读取“0”)的属性 - Uncaught TypeError: Cannot read properties of undefined (reading '0') in Simple React Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM