0

I have two functions that query the twitter api. The query is done through a web interface and I call each function when a checkbox is on. My problem now is after all the querying has been done, I want to be able to store the data and send it back to the web interface. How do I do this ?

    if (string.checktweet1 == 'on') {
      tweets(string.teamname)
    }
    if (string.checkmentions1 == 'on'){
      mentions(string.teamname)
    }
    if (string.checktweet2 == 'on'){
      tweets(string.playername)
    }

    if (string.checkmentions2 == 'on'){
      mentions(string.playername)
    }

      function mentions(x){
         client.get('search/tweets', {q:x, count:1},
         function (err,data,){
            for(var index in data.statuses){
               var tweet = data.statuses[index];
               console.log(tweet.text);
            }
       })
     }

My code is only sending the data for the function "tweets"

      json = {};
     function tweets(y){
       client.get('statuses/user_timeline', {screen_name:y, count:1},
       function(err,data) {
         for(var index in data){
           var tweet = data[index];
           console.log(tweet.text);
         }
          json[index] = tweet
          res.end(JSON.stringify(json));
      })
    }
qwerty ayyy
  • 385
  • 1
  • 2
  • 19
  • Is this your server side code? Are you using any database? You could read/write from/to a `JSON` file for dev purposes – cl3m Apr 04 '16 at 17:39
  • @cl3m this is server side code. I am not using a database. I just want to send the data I receive from the twitter api straight to my web interface – qwerty ayyy Apr 04 '16 at 17:43
  • How do you call your server code from your web interface? Are you using some kind of Ajax call? – cl3m Apr 04 '16 at 17:55
  • @cl3m yes i am using ajax. – qwerty ayyy Apr 04 '16 at 17:57
  • Can you post your client side code as well? You could do `$.post(url, data).done(function(response) { /* do something with response return by your server */ }).failed(function(err) { /* do something with err })` – cl3m Apr 04 '16 at 18:03
  • console.log() the errors to find out what is happening – vkstack Apr 04 '16 at 18:29

2 Answers2

1

As I understand you are not looking to save the data, but just collect the results of multiple asynchronous calls and once all are completed deliver the data to your client? If so, you could use async or promises.

There are already examples of this in Stack Overflow, eg. this Node.js: Best way to perform multiple async operations, then do something else? but here anyways simplified implementations for both.

Using async

var async = require('async');

// ...

var tweets = function(y) {
  return function(cb) {
    client.get('statuses/user_timeline', {screen_name: y, count: 1},
      function(err, data) {
        // Process the data ...
        cb(null, processedTweets);
      }
    );
  }
};

var mentions = function(x) {
  return function(cb) {
    client.get('search/tweets', {q: x , count: 1}, 
      function(err, data) {
        // Process the data ...
        cb(null, processedMentions);
      }
    );
  }
};

app.get('/mytweetsapi', function(req, res) {
  var tasks = [];
  if (string.checktweet1 == 'on') {
    tasks.push(tweets(string.teamname));
  }
  if (string.checkmentions1 == 'on'){
    tasks.push(mentions(string.teamname));
  }
  if (string.checktweet2 == 'on'){
    tasks.put(tweets(string.playername));
  }
  if (string.checkmentions2 == 'on'){
    tasks.put(mentions(string.playername));
  }

  async.parallel(tasks, function(err, results) {
    // Process the results array ...
    res.json(processedResults);
  });
});

Using promises

var Promise = require('bluebird');

// ...

var tweets = function(y) {
  return new Promise(function(resolve, reject) {
    client.get('statuses/user_timeline', {screen_name: y, count: 1},
      function(err, data) {
        // Process the data ...
        resolve(processedTweets);
      }
    );
  });
};

var mentions = function(x) {
  return new Promise(function(resolve, reject) {
    client.get('search/tweets', {q: x , count: 1}, 
      function(err, data) {
        // Process the data ...
        resolve(processedMentions);
      }
    );
  });
};

app.get('/mytweetsapi', function(req, res) {
  var tasks = [];
  // Same if this then tasks.push as in async example here

  Promse.all(tasks).then(function(results) {
    // Process the results array ...
    res.json(processedResults);
  });
});

I don't know what HTTP client you are using, but you can maybe use var client = Promise.promisifyAll(require('your-client-lib')); to convert the methods to return promises, and then you could convert the tweets and mentions functions to

var tweets = function(y) {
  return client.get('statuses/user_timeline', {screen_name: y, count: 1});
};

This way though the results in Promise.all are mixed responses and you would need to identify which are tweets and which are mentions to process them properly.

Community
  • 1
  • 1
vesse
  • 4,871
  • 26
  • 35
0

For saving the data to a file without a database, you can use fs.writeFile():

fs.writeFile('message.txt', 'Hello Node.js', (err) => {
  if (err) throw err;
  console.log('It\'s saved!');
});

https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback

Server/Node:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  if(twitterInfoReady){
      socket.emit('twitterInfoIncoming', { 
          twitterInfo1: 'blah',
          twitterInfo2: 'more data',
          twitterInfo3: 'and more data'
      });
  }
});

Client

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://localhost');
  socket.on('twitterInfoIncoming', function (data) {
    console.log(data.twitterInfo1);
  });
</script>

Example taken from Socket.io docs, just modified a tiny bit http://socket.io/docs/