简体   繁体   中英

Node,Express response body is Readablestream instead of JSON

Creating an API endpoint using Express. Usually responding with res.json() sends a JSON object in the response, which can consume in the client the same way.

I am using a batched promise in the API, resolving the promises (I think), and sending back an object.

Why is a JSON object not available client-side - instead I'm receiving a Readablestream in the body.

Below are the helper function and response, as well as what the client receives.

const addCoinVolumeData = async (data) => {
  const promises = data.map(async datum => {
    const volumeData = await fetchCoinGeckoCoinVolume(datum.id);
    const volumeDelta = calculateCoinVolumeDelta(volumeData);
    datum.volume_change_24h = volumeDelta;
    return datum;
  });

  const result = await Promise.all(promises);
  return result;
}
  data = await addCoinVolumeData(data);
  res.json(data);
Response {type: 'basic', url: 'http://localhost:3001/aggregate-coin-data', redirected: false, status: 200, ok: true, …}
body: ReadableStream
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"

Turns out it was actually the URL was using to fetch client-side since calling 'localhost...'. Proxy in package.json didn't resolve much. Changed the port, and for some reason that worked - don't know why since nothing else running on it (as far as was reported anyway). All is well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM