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();

            });

        });
4
  • 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. Commented Mar 22, 2016 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. Commented Apr 4, 2016 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. stackoverflow.com/questions/36408974/… Commented Apr 4, 2016 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. :) Commented Apr 4, 2016 at 17:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.