2017-09-22 123 views
-1

我正在研究一個Node.JS項目,我正在嘗試向API發出一個HTTP請求。我試圖調試這個代碼,並不知道爲什麼'responseData'沒有被設置。這個HTTP代碼爲什麼不返回任何數據?

var responseData = "initial"; 
var options = { 
    hostname:'api.insight.ly', 
    path: '/v2.1/contacts', 
    headers: { 
     'Authorization': 'Basic (API key here)', 
     'Accept-Encoding': 'gzip' 
    } 
}; 
http.get(options, (response) => { 
    const statusCode = response.statusCode; 
    let error; 
    if (statusCode !== 200) { 
     error = new Error('Request Failed.\n' + 
         `Status Code: ${statusCode}`); 
    } 
    if (error) { 
     responseData = "ERROR"; 
     // consume response data to free up memory 
     response.resume(); 
     return; 
    } 
     response.setEncoding('utf8'); 
     let rawData = ''; 
    response.on('data', (chunk) => rawData += chunk); 
    response.on('end',() => { 
    try { 
     const parsedData = JSON.parse(rawData); 
     responseData = parsedData 
    } catch (e) { 
     responseData = e.message; 
    } 
    }); 
}).on('error', (e) => { 
    responseData = (`Got error: ${e.message}`); 
    }); 
response.end(responseData); 

我相信我已經處理了每一個錯誤的情況下,無論如何,responseData應該成爲一個錯誤或JSON數據。但是,在此功能responseData的最後是初始。我錯過了什麼?

+0

因爲'http.get'是一個異步調用 - 在'http.get'回調中移動'response.end'。 – tymeJV

回答

0

您遇到範圍/關閉問題,並誤解了Javascript事件循環。此代碼不會按照您聲明的順序運行。您在第一行聲明並分配變量responseData。然後你聲明選項。然後,您告訴http核心模塊使用這些選項發起GET請求,併爲其提供回調(response)=>{...}。然後你通過responseDataresponse.end。在此範圍內,response甚至不存在;它作爲參數傳遞給您提供的http.get方法。事件監聽者(response.on('data'...,response.on('end'...)是真實行爲的地方。

你的代碼是在爲了真正執行的是這樣的:

-> assign responseData 
-> assign options 
-> call http.get 
-> call response.end with responseData 
-> call http.get callback 
-> set response.on 'data' callback 
-> set response.on 'end' callback 
-> call response.on 'data' callback with chunk1 
-> call response.on 'data' callback with chunk2 
-> etc... 
-> call response.on 'end' callback 
-> parse responseData 

其實,我不認爲你甚至真的需要一個response.end電話,似乎更有必要,如果你正在起草的響應是從發送您的服務器,因爲您真的只是想從另一個API發送的響應中累積數據。你真正需要的是在'結束'事件結束時通過積累的responseData的函數。

var responseData = "initial"; 
var options = { 
    hostname:'api.insight.ly', 
    path: '/v2.1/contacts', 
    headers: { 
     'Authorization': 'Basic (API key here)', 
     'Accept-Encoding': 'gzip' 
    } 
}; 
http.get(options, (response) => { 
    const statusCode = response.statusCode; 
    let error; 
    if (statusCode !== 200) { 
     error = new Error('Request Failed.\n' + 
         `Status Code: ${statusCode}`); 
    } 
    if (error) { 
     responseData = "ERROR"; 
     // consume response data to free up memory 
     response.resume(); 
     return; 
    } 
     response.setEncoding('utf8'); 
     let rawData = ''; 
    response.on('data', (chunk) => rawData += chunk); 
    response.on('end',() => { 
    try { 
     const parsedData = JSON.parse(rawData); 
     responseData = parsedData 
     //do something with your data here 
    } catch (e) { 
     responseData = e.message; 
    } 
    }); 
}).on('error', (e) => { 
    responseData = (`Got error: ${e.message}`); 
    }); 
相關問題