简体   繁体   English

使用MeteorJS将光纤中包裹的服务器端数据返回到客户端调用

[英]Returning Server Side Data Wrapped in a Fiber to a Client Side Call with MeteorJS

I am trying to get the server side method to return the tweet JSON objects in the browser console. 我正在尝试获取服务器端方法以在浏览器控制台中返回tweet JSON对象。 So far my app can pull information from the Twitter API and insert all of that data into a Collection but it won't return data to the call. 到目前为止,我的应用程序可以从Twitter API中获取信息,并将所有数据插入到Collection中,但不会将数据返回给调用。 I've done a bunch of tests with calls and methods to debug this issue and I think the Fiber might change how this call/method works. 我已经使用调用和方法调试了许多问题,以调试此问题,我认为光纤可能会更改此调用/方法的工作方式。

http://sf-tweet-locator.meteor.com/ http://sf-tweet-locator.meteor.com/

I want to be able to pull the longetude and latitude from each object so that I can place pins of the location of each tweet on a map. 我希望能够从每个对象中提取出经度和纬度,以便可以在地图上放置每个推文位置的图钉。 I'm not sure if the way that I am doing it is the "best" way but I am open to all suggestions! 我不确定我的做法是否是“最佳”方法,但是我愿意接受所有建议!

if (Meteor.isClient) {
  Meteor.call("tweets", function(error, results) {
    console.log(results); //results.data should be a JSON object
  });
}

Meteor.methods({
  Fiber = Npm.require('fibers');

  tweets: function(){
    Twit = new TwitMaker({
      consumer_key: '...',
      consumer_secret: '...',
      access_token: '...',
      access_token_secret: '...'
    });

    sanFrancisco = [ '-122.75', '36.8', '-121.75', '37.8' ];

    stream = Twit.stream('statuses/filter', { locations: sanFrancisco });

    var wrappedInsert = Meteor.bindEnvironment(function(tweet) {
      var userName = tweet.user.screen_name;
      var userTweet = tweet.text;
      console.log(userName + " says: " + userTweet);
      Posts.insert(tweet);
      return tweet;

    }, "Failed to insert tweet into Posts collection.");

    stream.on('tweet', function (tweet) {
      wrappedInsert(tweet);
      return tweet;
    });
  }, 
})

You could do it as a publication - that way you're not inserting useless data into your Posts collection, 您可以将其作为出版物-这样就不会在您的Posts集合中插入无用的数据,

This example creates a publication, which sends each new tweet to the local-tweets collection on the client. 本示例创建一个发布,该发布将每个新的tweet发送到客户端上的local-tweets集合。

You could possibly pass a queryId to the subscription/publication, and return that in each tweet document if you need to reference the source query. 您可以将queryId传递给订阅/发布,如果需要引用源查询,则可以在每个tweet文档中返回该queryId

if (Meteor.isClient){
  var localTweets = new Meteor.Collection('local-tweets');

  Meteor.subscribe("tweets", [ '-122.75', '36.8', '-121.75', '37.8' ]);

  localTweets.observe({
    added: function(doc) { console.log('tweet received!', doc); }
  });
}

if (Meteor.isServer){
  Meteor.publish("tweets", function(bbox){
    var pub = this, stopped = false;
    var Twit = new TwitMaker({
      consumer_key: '...',
      consumer_secret: '...',
      access_token: '...',
      access_token_secret: '...'
    });
    stream = Twit.stream('statuses/filter', { locations: bbox });

    var publishTweet = Meteor.bindEnvironment(function(tweet) {
      if (stopped) return;
      pub.added("local-tweets", Random.id() /* or some other id*/, tweet);
    }, "Failed to tweet.");

    stream.on('tweet', publishTweet);

    this.ready()

    this.onStop(function(){
      /* any other cleanup? */
      stopped = true;
    });
  });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM