[Node.JS] troubleshootiog issue: Error: TypeError: (intermediate value) is not iterable

박두팔이·2024년 3월 14일
0

Node.JS

목록 보기
10/20
  • error code
    /co2024-03-12 17:37:38:3738 [deviceRepository.js] ERROR: Failed to initialize client status for device 'Avalve-eunhee' in the database. Error: TypeError: (intermediate value) is not iterable
    C:\Users\parkeunhee\Desktop\AVALVE\Avalve_DataServer(re)\src\repository\deviceRepository.js:51
    const [result] = await dbConnection.query(sql, params);
    ^
    
    TypeError: (intermediate value) is not iterable
    at Object.initializeDeviceState (C:\Users\parkeunhee\Desktop\AVALVE\Avalve_DataServer(re)\src\repository\deviceRepository.js:51:30)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

🚨 문제 발생 상황

클라이언트의 소켓 연결이 끊기면서 서버가 같이 error로 인해 종료되는 문제


☑️ 문제 원인

  • 클라이언트의 소켓에서 disconnect 라는 이벤트가 발생하면 deviceRepository.initializeDeviceState() 메서드는, 연결되어있는 데이터베이스에 쿼리문을 실행하여 값을 초기화 한다.
    • HTTP_token = null로 초기화
    • Session_exise = 0으로 초기화
  • 에러 코드에서 나온 is not iterable 문장은, 아래의 코드 라인에서 쿼리문을 수행한 결과 [result] 변수에 할당하려고 했으나 디스트럭처링에 실패했다는 의미다.
    const [result] = await dbConnection.query(sql, params);
  • 디스트럭처링은, 배열이나 객체에서 값을 추출하여 개별 변수에 할당하는 기능을 의미한다.

  • 따라서 쿼리문이 디스트럭처링에 실패한 이유는, 쿼리 실행 결과가 하나의 Record(행)이기 때문에 [result]라는 변수에 할당하지 못하는 문제로 인한 error였다.


🪄 문제 해결 방법

  • result라는 변수를 선언 할 때, [result]가 아닌 result로 선언하여 레코드가 할당될 수 있도록 하여 디스트럭처링이 발생하지 않도록 하는 것이다.
    const result = await dbConnection.query(sql, params);

✨ 실행 결과

2024-03-14 09:18:38:1838 [app.js] INFO: Avalve-eunhee disconnected and client status reset

  • 클라이언트에서 소켓 연결을 끊었을 때, 클라이언트와의 연결이 끊기고 session_exist와 per_access가 초기화 되었음을 로그를 통해 알 수 있다.

profile
기억을 위한 기록 :>

0개의 댓글