Node.js如何使用回调将数据发送到Web界面 [英] Node.js how to use callback to send data to web interface
问题描述
我有两个函数查询twitter api。查询是通过Web界面完成的,当复选框打开时,我调用每个函数。我的问题现在是所有的查询已经完成后,我想要能够存储数据,并发送回到Web界面。如何做到这一点?
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);
}
})
}
我的代码只发送函数tweets的数据
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));
})
}
推荐答案
根据我的理解,你不是想保存数据,而是收集多个异步调用的结果,一旦所有的完成都将数据提交给您的客户端?如果是,您可以使用异步或 promises 。
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.
在Stack Overflow中已经有了这样的例子,例如。此 Node.js:最佳方法来执行多个异步操作,然后做其他的事情吗?但这里总是简化两个实现。
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.
使用异步 p>
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);
});
});
使用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);
});
});
我不知道你使用的HTTP客户端,但你也许可以使用 var client = Promise.promisifyAll(require('your-client-lib'));
将方法转换为返回promises,然后可以转换 tweets
和提供
函数
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});
};
这样虽然结果
c $ c> Promise.all 是混合响应,您需要确定 tweets
哪些是提及
以正确处理它们。
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.
这篇关于Node.js如何使用回调将数据发送到Web界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!