我正在尝试使用Twitter REST API GET关注者/ ids并使用Mongo集合插入方法将其保存到Mongo
/server/server.js中的代码:
boost::variant
服务器控制台出错:
Meteor.startup(function () {
// code to run on server at startup
TwitterFollowersIDsCollecions = new Mongo.Collection("twitterFollowersIDs");
var Twit = Meteor.npmRequire('twit');
var T = new Twit({
consumer_key: '###',
consumer_secret: '###',
access_token: '###',
access_token_secret: '###'
});
var getTwitterFollowersIDsAsync = function (screenname, cb) {
T.get('followers/ids', { screen_name: screenname }, function (err, data, response) {
console.log(data);
var vids = data.ids;
for(var i in vids) {
TwitterFollowersIDsCollecions.insert({
twitterFollowerID:vids[i]
});
}
}
);
};
Meteor.methods({
getTwitterFollowersIDsCollectionsClient : function (screenname){
var getTwitterFollowersIDsNow = Meteor.wrapAsync(getTwitterFollowersIDsAsync);
var result = getTwitterFollowersIDsNow('meteorjs');
console.log(result);
return result;
}
});
});
目标是将Twitter粉丝保存到Mongo集合中。
Meteor v.1.1.0.2
流星套餐:
Error: Meteor code must always run within a Fiber.
Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
:meteorhacks:npm
在meteor内部使用npm模块(在packages.json中添加)
**更新第二次尝试**
"twit": "1.1.20"
然后我从浏览器控制台调用以下代码:
Meteor.startup(function () {
// code to run on server at startup
TwitterFollowersIDsCollecions = new Mongo.Collection("twitterFollowersIDs");
var Twit = Meteor.npmRequire('twit');
var T = new Twit({
consumer_key: '###',
consumer_secret: '###',
access_token: '###',
access_token_secret: '###'
});
Meteor.methods({
// this is the server method called from the client
getTwitterFollowersIDsCollectionsClient : function (){
setTimeout(function(){
Meteor.call('getTwitterFollowersIDsNow', 'meteorjs');
},10);
return;
},
getTwitterFollowersIDsNow : function (screenname) {
T.get('followers/ids', { screen_name: screenname }, function (err, data, response) {
console.log(data);
});
}
});
});
服务器崩溃并出现同样的错误:
Meteor.call('getTwitterFollowersIDsCollectionsClient');
UDPATE:
Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
将Meteor.bindEnvironment添加到T.get回调方法。此代码有效,我能够将关注者ID保存到mongo集合
答案 0 :(得分:2)
很高兴你得到它的工作,但我玩这个和Meteor提供另一个,超级简单的方法:wrapAsync。至少,一旦我弄清楚它就很容易了!这是我用的服务器代码 -
var T = new TwitMaker({
consumer_key: '...'
, consumer_secret: '...'
, access_token: '...'
, access_token_secret: '...'
})
var wrapGet = Meteor.wrapAsync(T.get, T);
Meteor.methods({
getTwitImg: function(target) {
data = wrapGet('users/show', {screen_name: target});
if (data) {
img_url = data['profile_image_url'];
US.update({twitter: target}, {$set: {'targetImg': img_url}});
return img_url;
}
}
});
对于客户端和模板代码,请参阅此要点:https://gist.github.com/DanAncona/a09ce375e48bfa8efeca
答案 1 :(得分:0)
您的代码有点令人困惑。您似乎正在尝试执行Web服务调用异步,但仍然会立即返回结果(这不会起作用)。
首先,你可能不需要包装该函数来获取异步块中的追随者。
如果您希望服务器方法在调用后立即向客户端返回某些内容,我会使用Meteor.setTimeout
(请参阅What's the point of Meteor.setTimeout() vs just setTimeout()?)块并调用其他方法来执行取:
Meteor.methods({
// this is the server method called from the client
getTwitterFollowersIDsCollectionsClient : function (screenname){
Meteor.setTimeout(function() {
Meteor.call('getTwitterFollowersIDsNow', 'meteorjs');
}, 10);
return;
},
getTwitterFollowersIDsNow : function (screenname) {
T.get('followers/ids', { screen_name: screenname }, function (err, data, response) {
console.log(data);
var vids = data.ids;
for(var i in vids) {
TwitterFollowersIDsCollecions.insert({
twitterFollowerID:vids[i]
});
}
}
}
});
理想情况下,您可以使用模板助手从您的收藏集中检索您的关注者。由于这些助手是被动的,你可以从客户端调用服务器方法,让Meteor的反应性解决你通过帮助器返回追随者的问题(在数据更改时重新执行/重新渲染模板) )。
答案 2 :(得分:0)
尝试致电:
point.sqrmPrice <- ggplot(overview.df, aes(x = areaSize, y = sqrmPrice)) +
geom_line() +
geom_point() +
scale_y_continuous(breaks = c(seq(min(overview.df$sqrmPrice), max(overview.df$sqrmPrice), by = 10000) )) +
theme_bw()
来自api回调
var wrappedInsert = Meteor.bindEnvironment(function(tweet) {
TweetsCollection.insert(tweet);},
"Failed to insert tweet into Posts collection.");