yamoo9 / likelion-FEQA

질문/답변 — 프론트엔드 스쿨, 멋사
29 stars 9 forks source link

[LAB-10] The top-level-await experiment is not enabled 에러관련 질문입니다. #224

Closed hyunzsu closed 1 year ago

hyunzsu commented 1 year ago

질문 작성자

현지수

문제 상황

스크린샷 2023-03-24 오후 3 35 59 스크린샷 2023-03-24 오후 3 36 12

프로젝트 저장소 URL

환경 정보

hyunzsu commented 1 year ago

godlife.zip node_modules 제외한 프로젝트 파일 첨부합니다!

yamoo9 commented 1 year ago

문제 상황

The top-level-await experiment is not enabled 에러가 발생합니다.

문제 해결

await를 async 함수 외부에서 사용할 수 없어 발생한 오류입니다. 사이드 이펙트이므로 useEffect 훅 내부에서 실행해야 합니다. 아래 이미지와 코드를 참고해보세요. 😊

image

CurrentChallengeContainer.jsx

import { CurrentChallengeCard } from 'components';
import { doc, getDoc } from 'firebase/firestore';
import { dbService as db } from 'fbase';
import { useEffect } from 'react';

function CurrentChallengeContainer() {
  useEffect(() => {
    const docRef = doc(db, 'challenges2', '1');

    (async () => {
      const docSnap = await getDoc(docRef);

      try {
        if (docSnap.exists()) {
          console.log('Document data:', docSnap.data());
        } else {
          console.log('No such document!');
        }
      } catch (error) {
        console.log('Error getting document:', error);
      }
    })();
  }, []);

  return (
    <div className="w-[390px] bg-background px-[28px]">
      <CurrentChallengeCard />
    </div>
  );
}

export default CurrentChallengeContainer;