2

I am a newbie to node.js but I am getting a "write after end" error and I am not sure why. I know there are other similar questions but none of them provide a working solution to my problem I am trying to query the twitter api.

req.on('end', function() {
      var string = JSON.parse(body);
      res.writeHead(200, {
        "Content-Type": "application/json"
      });
      res.end(body);

      var tweetsArray = [];
      var finalTweets = [];

      client.get('search/tweets', {
          q: string.teamname,
          count: 1
        },
        function searchTweets(err, data, listStatuses) {
          for (var index in data.statuses) {
            var tweet = data.statuses[index];
            tweetsArray.push(JSON.stringify(tweet.text));
          }
          /* Callback function to query Twitter for statuses*/
          client.get('statuses/user_timeline', {
              screen_name: string.teamname,
              count: 1
            },
            function listStatuses(err, data, response) {
              for (var index in data) {
                var tweet = data[index];
                tweetsArray.push(JSON.stringify(tweet.text));
              }
              var tweets = JSON.parse(tweetsArray[0]);
              var tweetsB = JSON.parse(tweetsArray[1]);

              finalTweets = tweets.concat(tweetsB);

              res.write(JSON.stringify(finalTweets));

              res.end();

            });

        });
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
qwerty ayyy
  • 385
  • 1
  • 2
  • 19
  • 4
    Have you tried replacing `res.end(body)` with `res.write(body)`? Your error states you're writing after ending and, well, you're "ending" right at the beginning. So maybe try fixing that. – Mike Cluck Mar 22 '16 at 19:29
  • Hello qwerty ayyy, I noticed that you have never accepted any answer for your questions. While this feature is optional, please consider using it such that future readers of your questions are pointed towards the best answer. – timgeb Apr 04 '16 at 17:28
  • @timgeb thank you I will do that in the future. I have a new problem if you can help me solve it, I will be grateful. http://stackoverflow.com/questions/36408974/node-js-how-to-use-callback-to-send-data-to-web-interface – qwerty ayyy Apr 04 '16 at 17:31
  • No problem, make sure to go through your other questions as well (if you want to). I know very little javascript, so I can't help you with that. :) – timgeb Apr 04 '16 at 17:33

2 Answers2

2

You are calling res.end() near the beginning of your router. This ends the response, and then once your callback is executed you are writing to the response again with res.write(JSON.stringify(finalTweets));.

response.end([data][, encoding][, callback])

This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.

roflmyeggo
  • 1,724
  • 16
  • 18
0

You call res.end(body); before even invoking client.get('search/tweets'. Then you have res.write(JSON.stringify(finalTweets)); (and also res.end() again). You can't close the http response and then write to it.

Josh C.
  • 4,303
  • 5
  • 30
  • 51