codestates / codestates/aneun-dongne

[Error Handling Log] 외부 api를 반복문을 이용해 연속으로 요청할 때 에러가 뜨는 현상/손승이

Open
#79 3 comments 0 reactions 0 assignees View on GitHub
Error Handling Log server
Dominant language
JavaScript
Stars
6
Forks
7
PR merge metrics
No merged PRs in 30d

Description

> ### 어떤 에러인가요?
> 카카오 api를 이용해 Posts 테이블에 있는 모든 위도와 경도 정보에 대응되는 wtm 평면좌표를 받아올 때 data[i].post_contentid를 읽을 수 없다는 에러가 뜬다
> ### 에러 메시지
> ```shell
> $ cannot read properties of undefined (reading data[i].post_contentid)
> ```
> ### 에러 핸들링 방법
> 외부 api는 빠른 속도로 연속으로 요청할 수 없도록 제한이 걸려 있다.
> setTimeout()을 이용해 0.2초 간격으로 요청하도록 코드를 작성했다.
> ```js
> for (i = 0; i < data.length; i++) {
> (function (x) {
> setTimeout(function () {
> const options = {
> method: "GET",
> url: `https://dapi.kakao.com/v2/local/geo/transcoord.json?>x=${data[x].post_mapx}&y=${data[x].post_mapy}&input_coord=WGS84&output_coord=WTM`,
> headers: { Authorization: `KakaoAK ${process.env.REACT_APP_REST_API}` },
> };
> request(options, function (error, response, body) {
> if (error) {
> throw new Error(error);
> }
> let info = JSON.parse(body);
> Post.update(
> {
> post_wtmx: info.documents[0].x,
> post_wtmy: info.documents[0].y,
> },
> { where: { post_contentid: data[x].post_contentid } }
> );
> });
> }, 200 * x);
> })(i);
> }
> ```
> setTimeout()을 이용해 데이터베이스에 post_wtmx, post_wtmy를 저장하는 로직
> ### 에러 핸들링을 위해 참고한 레퍼런스 링크
> https://myhappyman.tistory.com/20

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the loop that calls Kakao's transcoord endpoint and the Post.update operation shown in the issue. Reproduce repeated requests using the provided setTimeout pattern and inspect the API response before reading documents[0]. Done means the requests complete without the undefined-property error and post_wtmx and post_wtmy are saved for each post.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.