An attempt to use the MeteorJS module w / twit Node, error: [TypeError: Object # <Object> does not have a method request]

So, I use MeteorJS w / twit Node to access the name of the tweet screen. Still just checking the code to see if I can extract the JSON from twitter.

Here is my code:

var Tget = Meteor.wrapAsync(T.get); Meteor.methods({ 'screenName' : function() { try { var result = Tget('search/tweets', {q:'#UCLA',count:1}); JSON.stringify(result); console.log(result); } catch (e) { console.log(e); return false; } } }) 

The error I am getting is:

  [TypeError: Object #<Object> has no method 'request'] 

Here is the git tweet module: https://github.com/ttezel/twit/blob/master/README.md

+2
source share
1 answer

I think I understand. Here is the T.get code :

 Twitter.prototype.get = function (path, params, callback) { return this.request('GET', path, params, callback) } 

As you can see, it expects this receive the request method. However, since we used wrapAsync without caring about the execution context ( access to this ), it fails.

Consider this example (you can copy / paste it into your browser console):

 var obj = { foo : 'foo', logThis : function() { console.log(this); } }; 

If we execute obj.logThis() , we have: Object { foo: "foo", logThis: obj.logThis() }
But if we do the following ...

 var otherLogThis = obj.logThis; otherLogThis(); 

It registers the Window object because we got this function from its context!

How to solve this problem? Link function? Difficult challenge?
No, Meteor has a solution. wrapAsync can have two parameters ... The second is context!

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

If you want to know more about JavaScript contexts, I suggest this book:
https://github.com/getify/You-Dont-Know-JS/
It is free and open source, and I am in no way affiliated with anything other than my deepest affection and fond memories of how my brain grows in every funny way when I read it.

+2
source

All Articles