• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

강사님 질문이있습니다.

22.02.11 02:27 작성 조회수 147

0

const test  = {

    "_declaration": {

        "_attributes": {

            "version": "1.0",

            "encoding": "UTF-8",

            "standalone": "yes"

        }

    },

    "response": {

        "header": {

            "resultCode": {

                "_text": "00"

            },

            "resultMsg": {

                "_text": "NORMAL SERVICE."

            }

        },

        "body": {

            "items": {

                "item": [

                    {

                        "as1": {

                            "_text": "경상남도"

                        },

                        "as2": {

                            "_text": "창원마산회원구"

                        },

                        "as3": {

                            "_text": "내서읍"

                        },

                        "as4": {

                            "_text": "삼계리"

                        },

                        "bjdCode": {

                            "_text": "4812725028"

                        },

                        "kaptCode": {

                            "_text": "A63085232"

                        },

                        "kaptName": {

                            "_text": "마산삼계2"

                        }

                    },

                 

            },

            "numOfRows": {

                "_text": "10"

            },

            "pageNo": {

                "_text": "1"

            },

            "totalCount": {

                "_text": "18496"

            }

        }

    }

}

노드기반으로 공공api를 xml 구조이길래  xml2json 모듈을 이용해서 json 구조로 바꾼다음에

sequelize 모델에 create 해주려고하는데

 JSON 구조의 값을 test['response']['body']['items']['item'] 요런구조로 못받나요?

자꾸 console찍어보면 undefined 가나오네요

답변 4

·

답변을 작성해보세요.

0

jaeyoung Lee님의 프로필

jaeyoung Lee

2022.02.17

강사님 들은 수업 기반으로 프론트 + 백엔드 합쳐서 토이프로젝트 진행중에 막히는점을 질문

드려도괜찮을까요? 

다름이아니구 제가 카카오에서 사용하는 좌표 x, y 를 사용하기위해서 

mysql db에 저장된 400 ,500 건정도 row를 조회후 x, y만 가져와서 테이블에 업데이트 해주려고 

하는데 이 요청을보내면  429 Too Many Requests 오류가뜨고 

for문도 첫번째는 돌아서 업데이트가 되긴하는데 이점은 해결방법이 있나요? 

 

 

 for(var i in noLocationList){
 

            const option = {
                url: `https://dapi.kakao.com/v2/local/search/address.json?query=${encodeURI(noLocationList[i]['address'])}`,
                methods: 'GET',
                headers : {
                    Authorization : 'KakaoAK 064940e08c54717d89d8728298c08b71'
                },
                // query : noLocationList[0]['address']
            }


            request(option, async (error,response) =>{
                console.log("호출")
                console.log("에러내용",error);
                console.log(response);
                if(error) new Error('400');
                // console.log("===========몇번쨰리퀘스트???",i);
                var result = JSON.parse(response.body);
               
                //  result.documents[0].x   // result.documents[0].y
                // var jsonVal = xmlToJson(response.body);
                // console.log(jsonVal);
               
                await ApartmentPrice.update({
                    xlocation :  result.documents[0].x ,
                    ylocation : result.documents[0].y
                },
                {where : {id: noLocationList[i]['id']}})
             })  
      }

남의 api를 쓰는 이상 사용량 제한이 있다면 특정 시간 간격을 두고 요청을 보내는 수밖에 없습니다.

0

jaeyoung Lee님의 프로필

jaeyoung Lee

2022.02.11

if(testVal[i]['as1']===undefined) {testVal[i]['as1']={_text:''} }
 
 
객체 가 비었을시 value가 빈값을 세팅해주어서 해당문제 해결했습니다.

0

required가 false면 필수값이 아니라서 에러가 발생하지 않을텐데요? 어떤 에러가 발생하시나요?

시퀄라이즈문에 관해서는 create를 여러 개 하는 거라면 

https://stackoverflow.com/questions/29461908/how-to-do-bulk-insert-using-sequelize-and-node-js

bulkCreate를 사용하시고요.

그게 아니라 순서대로 가는거라면 굳이 묶을 필요가 없습니다. 반드시 동시에 되어야하는 것만 promise.all하시면 됩니다.

0

jaeyoung Lee님의 프로필

jaeyoung Lee

2022.02.11

해당문제는 JSON.parse(test)로 해결했습니다.

 

강사님 질문이있는데요 

 

 for(i in testVal){


        // console.log(testVal[i]['kaptName']['_text'])
        // console.log(testVal[i]['as1']['_text']);
        // console.log(testVal[i]['as2']['_text']);
        // console.log(testVal[i]['as3']['_text']);
        // console.log(testVal[i].as4._text);
        Apartment.create({
            kaptName: testVal[i]['kaptName']['_text'],
            as1 : testVal[i]['as1']['_text'],
            // as2 : testVal[i]['as2']['_text'],
            as3 : testVal[i]['as3']['_text'],
        //     as4 : testVal[i]['as4']['_text'] || '',
           
      });
 
현재 testVal[i].as4가 required가 false라서 빈값이 올수도있는데
이런게 오류가 안되게하려면 어떻게 처리해줘야하나요?
 
그리고 여러가지 문 시퀄라이즈는 promise.allsettled 혹은 promise.all써줘야할까요?