Another wrapAsync Problem + Question

I am using the twit npm package and having trouble with using it asynchronously in a method.

I get the error :
“Exception while invoking method ‘getTweets’ TypeError: this.request is not a function”

Meteor.methods({
  'getTweets': function () {
      var T = new Twit({
          consumer_key:         'my_key_works',
          consumer_secret:      'my_secret_works',
          access_token:         'my_token_works',
          access_token_secret:  'my_secret_token_works'
      });
	  
	  var node = 'statuses/user_timeline';
	  var query = {
		  q: 'screen_name%3Atwitter',
		  count: 5,
		  exclude_replies: true,
	  };
	  var callback = function(err, data) {
		  if(err){
			  console.log(err);
		  }
		  else{
			  var objs = [];
			  for (i=0;i<data.length;i++){
				  var obj = {
					  created_at: data[i].created_at,
					  text: data[i].text,
					  user_name: data[i].user.name,
					  user_screen_name: data[i].user.screen_name,
					  user_url:  data[i].user.url
				  };
				  objs.push(obj);
			  }
			  objs = JSON.stringify(objs);
			  return(objs);
		  }
	  });

      var c = Meteor.wrapAsync(T.get);
      var res = c(node,query,callback);
	  return res;
  }
});

Template.Tweets.onRendered(function () {
    Meteor.call('getTweets', {}, function(error, result){
        if(error){
            console.log(error);
        }else{
            console.log(result);
        }
    });
});

I also tried

var res = Meteor.wrapAsync(T.get(node,query,callback));
	  return res;

but client side i got undefined - as if the async was not working

var c = Meteor.wrapAsync(T.get, T);

is most likely the answer.

However, the twit api supports Promises, so you may find using async/await simpler.

In the end I switched to the async await - and it worked great.

1 Like