0

I have function queryIterator that returns async object value called result, I use a call back function to send result to the browser via res.send().

Error: (node:3497) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I think im getting error because i am consistently resending a response to the browser when the header is already set, correct?

How can I continuously stream data to the client browser from express server?

  router.post('/', async (req, res) => {
    // Assign first query
    searchedT = req.body.searchTerm;
    queries.push(req.body.searchTerm);
    // Invoke the async iterator
    console.log("start....");
    const results = await queryIterator(queries, async function(result) {
      console.log("--->" + await result);
      res.send(await result);

    });
    // res.send(result);
});


async function queryIterator(queries, callback) {
  for await (const result of queryGenerator()) {
      // console.log('Iterating... ', result);
      // console.log(`On Queue Searches #${++counter}: `, queries);
      // console.log(`On Queue Searches LAST #${++counter}: `, queries[queries.length-1]);
       if (!queries.length) {
          console.log("query length is zero");
          return result;
      }
      callback(result);
      // return result;
  }
}
Shaz
  • 1,443
  • 1
  • 27
  • 67
  • Why dont you get all the results first then send them? – Nelson Owalo Oct 08 '18 at 08:10
  • Because this is essentially an infinite loop of api calls @PlatinumIndustries – Shaz Oct 08 '18 at 08:14
  • Then why don't you just send the result as a stream? – Nelson Owalo Oct 08 '18 at 08:18
  • or if you want to make a two way connection, you can go for sockets too. – Atishay Jain Oct 08 '18 at 08:22
  • @AtishayJain New to Javascript and Async, Im not sure what you mean, can you please demonstrate an example? – Shaz Oct 08 '18 at 08:24
  • Correct, `send()` sets some headers (like content length), writes a body, and ends the response. You can't do this multiple times. Use `write` instead. – Bergi Oct 08 '18 at 11:29
  • @Bergi Getting this: (node:3180) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string or Buffer. Received type object – Shaz Oct 09 '18 at 01:15
  • when Changed to this: res.write(await result); – Shaz Oct 09 '18 at 01:16
  • You should not use an `async function` as a callback, and there's no need to `await` the `result`. But you need to convert it to a string - maybe `JSON.stringify()`? I don't know what kind of stream you want. – Bergi Oct 09 '18 at 08:05
  • @Bergi the problem with write is it shows results after the post is complete i.e. status 200.. i want it to be streams as i get the values – Shaz Oct 09 '18 at 13:43
  • What do you mean by "*after the post is complete*"? How are you consuming the API, with [`EventSource`](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events)? Or no JS client at all? – Bergi Oct 09 '18 at 17:42
  • @Bergi After the post request is complete meaning its a form with POST method, that says, search tweets on twitter. The twitter API has an npm module with node, using basically that to request tweets, now want to stream the tweets to the browser - as I get them async data, – Shaz Oct 10 '18 at 00:43

0 Answers0