[英]Error with Twitter Nodejs query
I've tried a few variations of this snippet. 我已经尝试了此片段的一些变体。 I keep getting the same undefined error when trying to print a json object to the console.
尝试将json对象打印到控制台时,我一直收到相同的未定义错误。 Thanks in advance.
提前致谢。
var userTweets = client.get('statuses/user_timeline', function(error, tweets, response){
if(error) throw error;
return tweets
});
console.log(userTweets)
client.get
is asynchronous client.get
是异步的
This code is working, then you have to manage your code to wait for asynchronous response. 该代码有效,然后您必须管理代码以等待异步响应。
client.get('statuses/user_timeline', function(error, tweets, response){
if(error) throw error;
console.log(tweets)
});
If you have some code after client.get, you should do that 如果client.get之后有一些代码,则应该这样做
before : 之前:
var userTweets = client.get(..., function() {});
// do some code
console.log(userTweets);
after : 之后:
client.get(....., function(error, tweets) {
...
otherCode(tweets);
});
function otherCode(userTweets) {
// do some code
console.log(userTweets);
}
I suggest you to read this answer : https://stackoverflow.com/a/11233849/5384802 to understand asynchronous code. 我建议您阅读以下答案: https : //stackoverflow.com/a/11233849/5384802以了解异步代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.