我有一個 nodejs 應用程式,它使用http向外部 API 發送 http 請求并檢索 json 資料(以十六進制緩沖區的形式)作為回應。
但是,當我嘗試從 API 中提取大型資料集時,資料不完整。json 將切斷大約三分之一的路徑并嘗試決議它會產生錯誤。我不認為我的決議 (toString) 有問題,因為 res.complete 沒有被觸發,所以它顯然沒有完成。
有沒有辦法強制我的請求等待 res.complete 完成?
我的請求是這樣的:
const req = https.get(options=pull_options,res=> {
res.on('data', d=> {
if(res.complete) {
resolve(d.toString());
} else {
console.error("Connection terminated while message was still being sent.")
}
}
}
我真的不認為 API 將我拒之門外是個問題,因為我能夠在 python 中使用幾乎相同的代碼提取相同的資料集而沒有任何問題。
uj5u.com熱心網友回復:
output = '';
const req = https.get(options=pull_options,res=> {
res.on('data', d=> {
output = d;
}
res.on('end', d=> {
resolve(output)
}
}
將解決方案更改為 on end 以允許所有資料進入對我有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/534429.html
標籤:节点.js网址异步等待
