2017-11-05 4 views
1

私はexpress.jsでサーバーを構築しており、その一部は、以下のようになります。私は「API /ものを」ヒットするとエクスプレスサーバーからjsonを返すには?

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); 
    }) 
}); 

それはエラーを返します:

私は返すことができますどのように

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

私のエンドポイントの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); 
    }) 
}); 
として必要なデータを送信避けるために

関連する問題