React-Uncaught TypeError : Airtable을 사용하여 정의되지 않은 '이름'속성을 읽을 수 없습니다.

EsoMars

React사용자가 다른 선택 항목 중에서 찬성 투표를할지 여부를 결정할 수 있는 작은 설문 조사 구성 요소를 만들고 있습니다. 투표는 Airtable을 통해 "Survey"라는 테이블에 등록됩니다.

내가 가진 문제는 터미널에서 다음과 같은 오류가 발생한다는 것입니다 .TypeError : Cannot read property 'name'of undefined`

0 값을 가진 정의되지 않은 값과 관련된 것 같습니다. 그러나 나는 특정 배열을 'map ()'하고 정의되지 않은 것을 이해하지 못합니다.

Survey.js

const Survey = () => {
  const [items, setItems] = React.useState([]);
  const [loading, setLoading] = useState(true);

    const getRecords = async () => {
      const records = await base('Survey').select({}).firstPage().catch(err => console.log(err));
      // console.log(records);
      const newRecords = records.map((record) => {
        // lets destructure to get the id and fields
        const {id, fields} = record;
        return {id, fields};
      })
      setItems(newRecords);
      setLoading(false);
    }

  useEffect(() => {
    getRecords();
    console.log(items)
  },[])

  return (
    <Wrapper className="section">
      <div className="container">
        <Title title="Survey" />
        <h3>most important room in the house?</h3>
        {loading ? (
          <h3>loading...</h3>
          ) : (
          <ul>
            {items.length > 0 && items[0].name.first}
            {items.map(item => {
              console.log(items);
              const {
                id, 
                fileds: { name, votes },
              } =item;

              return (
                <li ley={id}>
                  <div className="key">
                    {name.toUpperCase().substring(0,2)}
                  </div>
                  <div>
                    <h4>{name}</h4>
                    <p>{votes} votes</p>
                  </div>
                  <button onClick={() => console.log("clicked")}>
                    <FaVoteYea />
                  </button>
                </li>
              )
            })}
          </ul>)}
      </div>
    </Wrapper>
  )
}

문제의 일부는 객체 배열의 초기화로 pourposly 설정된 라인 블로우에 값이 주어지지 않았기 때문이라고 생각합니다 (초기화하고 싶었 기 때문에).

  const [items, setItems] = React.useState([]);

결국 문제를 일으키는 부분은 다음과 같습니다.

   {items.length > 0 && items[0].name.first}
   {items.map(item => {
      console.log(items);
      const {
         id, 
         fileds: { name, votes },
      } =item;

지금 까지이 포스트다른 포스트를 공부했습니다 . 부분적으로는 마지막 것이 유용 해 보이지만 아직 작업 중이지만 아직 능숙 Angular하지 않고 완전히 이해 Typescript하지 못했습니다.

그런 다음 게시물 을 연구 했지만 정의되지 않은 변수에 대한 답을 찾을 수 없었습니다.

잠재적 인 솔루션을 안내해 주셔서 감사합니다.

안녕하세요 지구

ES6에서 구조화를 통해 코드를 간소화 할 수 있습니다.

그래서 보시다시피

const {
         id, 
         fileds: { name, votes },
      } =item

다음과 같다

item.id // some id
item.fileds.name // some name i
item.fields.votes // some votes

귀하의 경우에는 개체 항목을 분해하지만 파일은 항상 고전적인 형식입니다.

당신의 코드는

<h4>{fileds.name}</h4>
<p>{fileds.votes} votes</p>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Uncaught TypeError : proengsoft / laravel-jsvalidation을 사용하여 정의되지 않은 'indexOf'속성을 읽을 수 없습니다.

분류에서Dev

Uncaught TypeError : 정의되지 않은 React / Redux의 'type'속성을 읽을 수 없습니다.

분류에서Dev

Uncaught TypeError : onClick React에서 정의되지 않은 'openModal'속성을 읽을 수 없습니다.

분류에서Dev

Uncaught TypeError : 정의되지 않은 'history'속성을 읽을 수 없습니다 !! React 라우터

분류에서Dev

ReactDOM : Uncaught TypeError : 정의되지 않은 'render'속성을 읽을 수 없습니다.

분류에서Dev

Vuetify Uncaught TypeError : 정의되지 않은 'install'속성을 읽을 수 없습니다.

분류에서Dev

jqgrid Uncaught TypeError : 정의되지 않은 속성 'stype'을 읽을 수 없습니다.

분류에서Dev

JS Uncaught TypeError : 정의되지 않은 속성 'url'을 읽을 수 없습니다.

분류에서Dev

CkEditor : Uncaught TypeError : 정의되지 않은 'indexOf'속성을 읽을 수 없습니다.

분류에서Dev

Ajax Uncaught TypeError : 정의되지 않은 'length'속성을 읽을 수 없습니다.

분류에서Dev

ReactJS Uncaught TypeError : 정의되지 않은 'concat'속성을 읽을 수 없습니다.

분류에서Dev

Electron JavaScript Uncaught TypeError : 정의되지 않은 'on'속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 속성 "이름"을 읽을 수 없습니다.

분류에서Dev

Meteor : Uncaught TypeError : 정의되지 않은 '이벤트'속성을 읽을 수 없습니다.

분류에서Dev

React Jest : TypeError : 정의되지 않은 'map'속성을 읽을 수 없습니다.

분류에서Dev

React TypeError : 정의되지 않은 'getDates'속성을 읽을 수 없습니다.

분류에서Dev

React Native TypeError : 정의되지 않은 'timeSlots'속성을 읽을 수 없습니다.

분류에서Dev

Typescript, React : TypeError : 정의되지 않은 'push'속성을 읽을 수 없습니다.

분류에서Dev

React TypeError : 정의되지 않은 'map'속성을 읽을 수 없습니다.

분류에서Dev

UnhandledPromiseRejectionWarning : TypeError : Sequelize ORM을 사용하여 정의되지 않은 '생성'속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 '사용자'속성을 읽을 수 없습니다.

분류에서Dev

React-TypeError : 정의되지 않은 '이미지'속성을 읽을 수 없습니다.

분류에서Dev

메타 마스크가 window.ethereum을 주입하지 않습니다 : Uncaught (약속) TypeError : 정의되지 않은 '요청'속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 '페이지'속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 '이미지'속성을 읽을 수 없습니다.

분류에서Dev

TypeError : 정의되지 않은 '이미지'속성을 읽을 수 없습니다.

분류에서Dev

이슬비 사용 중 오류 useCacheCall () : TypeError : 정의되지 않은 'methods'속성을 읽을 수 없습니다.

분류에서Dev

Ionic-Uncaught (약속) : TypeError : 정의되지 않은 속성 'then'을 읽을 수 없습니다.

분류에서Dev

VueJS : Uncaught (약속) TypeError : 정의되지 않은 속성 'rol'을 읽을 수 없습니다.

Related 관련 기사

  1. 1

    Uncaught TypeError : proengsoft / laravel-jsvalidation을 사용하여 정의되지 않은 'indexOf'속성을 읽을 수 없습니다.

  2. 2

    Uncaught TypeError : 정의되지 않은 React / Redux의 'type'속성을 읽을 수 없습니다.

  3. 3

    Uncaught TypeError : onClick React에서 정의되지 않은 'openModal'속성을 읽을 수 없습니다.

  4. 4

    Uncaught TypeError : 정의되지 않은 'history'속성을 읽을 수 없습니다 !! React 라우터

  5. 5

    ReactDOM : Uncaught TypeError : 정의되지 않은 'render'속성을 읽을 수 없습니다.

  6. 6

    Vuetify Uncaught TypeError : 정의되지 않은 'install'속성을 읽을 수 없습니다.

  7. 7

    jqgrid Uncaught TypeError : 정의되지 않은 속성 'stype'을 읽을 수 없습니다.

  8. 8

    JS Uncaught TypeError : 정의되지 않은 속성 'url'을 읽을 수 없습니다.

  9. 9

    CkEditor : Uncaught TypeError : 정의되지 않은 'indexOf'속성을 읽을 수 없습니다.

  10. 10

    Ajax Uncaught TypeError : 정의되지 않은 'length'속성을 읽을 수 없습니다.

  11. 11

    ReactJS Uncaught TypeError : 정의되지 않은 'concat'속성을 읽을 수 없습니다.

  12. 12

    Electron JavaScript Uncaught TypeError : 정의되지 않은 'on'속성을 읽을 수 없습니다.

  13. 13

    TypeError : 정의되지 않은 속성 "이름"을 읽을 수 없습니다.

  14. 14

    Meteor : Uncaught TypeError : 정의되지 않은 '이벤트'속성을 읽을 수 없습니다.

  15. 15

    React Jest : TypeError : 정의되지 않은 'map'속성을 읽을 수 없습니다.

  16. 16

    React TypeError : 정의되지 않은 'getDates'속성을 읽을 수 없습니다.

  17. 17

    React Native TypeError : 정의되지 않은 'timeSlots'속성을 읽을 수 없습니다.

  18. 18

    Typescript, React : TypeError : 정의되지 않은 'push'속성을 읽을 수 없습니다.

  19. 19

    React TypeError : 정의되지 않은 'map'속성을 읽을 수 없습니다.

  20. 20

    UnhandledPromiseRejectionWarning : TypeError : Sequelize ORM을 사용하여 정의되지 않은 '생성'속성을 읽을 수 없습니다.

  21. 21

    TypeError : 정의되지 않은 '사용자'속성을 읽을 수 없습니다.

  22. 22

    React-TypeError : 정의되지 않은 '이미지'속성을 읽을 수 없습니다.

  23. 23

    메타 마스크가 window.ethereum을 주입하지 않습니다 : Uncaught (약속) TypeError : 정의되지 않은 '요청'속성을 읽을 수 없습니다.

  24. 24

    TypeError : 정의되지 않은 '페이지'속성을 읽을 수 없습니다.

  25. 25

    TypeError : 정의되지 않은 '이미지'속성을 읽을 수 없습니다.

  26. 26

    TypeError : 정의되지 않은 '이미지'속성을 읽을 수 없습니다.

  27. 27

    이슬비 사용 중 오류 useCacheCall () : TypeError : 정의되지 않은 'methods'속성을 읽을 수 없습니다.

  28. 28

    Ionic-Uncaught (약속) : TypeError : 정의되지 않은 속성 'then'을 읽을 수 없습니다.

  29. 29

    VueJS : Uncaught (약속) TypeError : 정의되지 않은 속성 'rol'을 읽을 수 없습니다.

뜨겁다태그

보관