I am using Axios to extract data from an API. Since there are so many records, I am trying to pipe the response into a variable. But I am not able to do it.
app.post("/Node", jsonparser, async (req, res) => {
var APIdata;
axios(authOptions)
.then((response) => {
response.data.pipe(APIdata);
})
.catch((error) => {
res.send(error);
});
}
Where as, I am able to send the data as a response to res and get it displayed in the frontend. The code for it is below.
app.post("/Node", jsonparser, async (req, res) => {
axios(authOptions)
.then((response) => {
response.data.pipe(res);
})
.catch((error) => {
res.send(error);
});
}
Could anyone please tell me how we can save the data to a variable? The data needs to be pushed to database...
You can do something like this,
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));
});