2014-09-05 90 views
1

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

//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 
     } 
    }); 
} 

有什么建议吗? :-)

回答

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(); 
     } 
    }); 
} 
+0

未来选项工作完美无缺!谢谢! :-) – 2014-09-08 14:52:23

0

容易,但有点肮脏的方式(但没有这么多,如果你想好你的架构) - >发回的结果低谷蒙戈。 你甚至可以在没有Meteor.methods的情况下做到这一点,创建请求在客户端的数据库中插入,服务器上的观察者检查并执行异步任务,然后将结果写回数据库。