2017-11-05 1 views
1

내가 express.js과 그것의 일부와 서버를 구축해야는 다음과 같습니다익스프레스 서버에서 json을 반환하는 방법은 무엇입니까?

(node:1626) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Converting circular structure to JSON

는 어떻게 반환 할 수 있습니다 내가 'API/물건을'공격 할 때

app.get("/api/stuff", (req, res) => { 
    axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){ 
    res.send(response); 
    console.log('response=',response); 
    }) 
}); 

가 오류를 반환 내 끝점의 json?

답변

2

오픈 날씨 API에서 가져온 response 개체는 원형 (개체 자체를 참조하는 개체)입니다. JSON.stringify은 순환 참조를 통해 오류가 발생합니다. 이것이 send 방법을 사용하는 중에이 오류가 발생하는 이유입니다.

이 그냥 응답으로 필요한 데이터를 전송 피하기 위해

app.get("/api/stuff", (req, res) => { 
    axios.get('http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1').then(function(response){ 
    res.send(response.data); 
    console.log('response=',response.data); 
    }) 
}); 
관련 문제