【问题标题】:how to push data back to client in meteor?如何在流星中将数据推送回客户端?
【发布时间】:2014-09-06 06:12:56
【问题描述】:

当用户单击按钮时,我必须对 DB 进行聚合查询,但是我不知道如何将该结果返回给客户端,因为我正在执行异步请求,这是我的代码的一部分:

//Server side
Meteor.startup(function() {
    Meteor.methods({
        getAllTotals: function (query){
            var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
            var error = result = match = pipeline = '';
            var group = {
              $group: {
                  _id: null,
                  wall_clock: {
                      "$sum": "$wall_clock"
                  },
                  mem:{
                      "$sum": "$mem"
                  },
                  cpu:{
                      "$sum": "$cpu"
                  },
                  io:{
                      "$sum": "$io"
                  },
                  vmem:{
                      "$sum": "$vmem"
                  },
                  maxvmem:{
                      "$sum": "maxvmem"
                  }
              }
           };

          if(typeof query.submission_time !== "undefined"){
              match = {"$match": {submission_time: query.submission_time}};
              pipeline = [match, group];
          }else{
              pipeline = [group];
          }

          db.collection("GE_qstat_job_monitor").aggregate(
              pipeline,
              Meteor.bindEnvironment(
                  function (error, result){
                      console.log(result); // <<--- this is OK!
                  },
                  function(error) {
                      Meteor._debug( "Error doing aggregation: " + error);
                  }
              )
          );
          return result; // <<--- this is empty
        }
    });
}

有什么建议吗? :-)

【问题讨论】:

    标签: meteor


    【解决方案1】:

    简答:

    您可以在这里找到解决方案:

    详细解答

    使用Meteor._wrapAsync

     var aggregateTotal = function(callback){
          var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
    
          // ...
    
          db.collection("GE_qstat_job_monitor").aggregate(
                  pipeline,
                  function (error, result){
                    if(error){
                       callback(error);
                    }else{
                       callback(null, result);
                    }
                  }
          );
      }
    
      var aggregateTotalsSync = Meteor._wrapAsync(aggregateTotal);
    
      Meteor.methods({
          'getAllTotals': function(){
              var result;
              try{
                   result = aggregateTotalsSync();
              }catch(e){
                  console.log("getAllTotals method returned error : " + e);
              }finally{
                  return result;
              }
          }
      });
    

    使用期货 (meteorPad example)

    //Server side
    Meteor.startup(function() {
        var Future = Npm.require('fibers/future');
        Meteor.methods({
            getAllTotals: function (query){
              var fut = new Future();
              var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
    
              // ...
    
              db.collection("GE_qstat_job_monitor").aggregate(
                  pipeline,
                  Meteor.bindEnvironment(
                      function (error, result){
                         if(error){
                           fut.throw(error);  
                         }else{
                           fut.return(result)
                         }
                      },
                      function (exception){
                         // caught exception is passed to this callback
                         fut.throw(exception);
                      }
                  )
              );
              return fut.wait();
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      简单但有点脏的方式(但如果你对你的架构考虑得很好,那就不是那么多了)-> 通过 Mongo 发回结果。 你甚至可以不使用 Meteor.methods,将请求创建插入客户端的数据库中,服务器上的观察者检查它并执行异步任务,然后将结果写回数据库中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-31
        • 2011-01-02
        • 1970-01-01
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 2014-05-05
        • 1970-01-01
        相关资源
        最近更新 更多