簡體   English   中英

使用流將響應通過管道傳輸到變量中 | 愛訊 | 節點.js

[英]Piping the response into a variable using streams | Axios | Node.js

我正在使用 Axios 從 API 中提取數據。 由於記錄太多,我試圖將響應傳遞到變量中。 但我做不到。

app.post("/Node", jsonparser, async (req, res) => {
var APIdata;
axios(authOptions)
    .then((response) => {
        response.data.pipe(APIdata);
    })
    .catch((error) => {
        res.send(error);
    });
}

其中,我可以將數據作為對res的響應發送,並將其顯示在前端。 它的代碼如下。

app.post("/Node", jsonparser, async (req, res) => {
axios(authOptions)
    .then((response) => {
        response.data.pipe(res);
    })
    .catch((error) => {
        res.send(error);
    });
}

誰能告訴我如何將數據保存到變量中? 數據需要推送到數據庫...

你可以做這樣的事情,

var APIdata;
axios.get(url, { responseType: "stream" }).then(response => {
const stream = response.data;

stream.on("data", chunk => {
  APIdata.push(chunk)
};

stream.on("end", () => console.log("end of stream", APIdata));
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM