Front-End프로그래밍

[Vue3&TypeScript] Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘forEach’)at UserTestPopup.vue:367:34 오류 수정방법


오류 메시지 분석

UserTestPopup.vue:367 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'forEach')

즉, 아래 코드에서 mainStore.userListundefined거나 null 상태일 때 .forEach()를 호출하고 있다는 뜻이에요.

mainStore.userList.forEach(item => {
  ...
});


해결 방법

userList가 배열인지 확인하는 방어 코드 추가

if (Array.isArray(mainStore.userList)) {

  const newMap: typeof selectedRecoveryData.value = {};

  mainStore.userList.forEach(item => {
    newMap[item.cardUserSeq] = {
      ...item,
      userId: props.userId ?? '',
      useYn: false
    };
  });

  selectedRecoveryData.value = newMap;
}
error: Content is protected !!