【问题标题】:Waiting for meteor cursor in method在方法中等待流星光标
【发布时间】:2016-12-22 06:11:36
【问题描述】:

我有一个大型聚合查询,要求我将“allowDiskUse:true”作为选项传递。这不适用于此处描述的聚合: https://github.com/meteorhacks/meteor-aggregate/issues/11

这里定义了我的流星方法。当我调用该方法时,我需要等待 ondata 完成,然后再将任何内容返回给客户端,但是我尝试的任何方法都无法让我以安全的方式将数据发送到前端。

Meteor.methods({
  'getSummary': function (dept,startDate,endDate,filterType) {

         f = myQuery(startdate,enddate,dayFinalGroup);   
        f.on("data", Meteor.bindEnvironment(function(row) {
//load an array or something here to return
            }));
            f.once("end", Meteor.bindEnvironment(function() {
                // tidy up, in my case end the stream

            }));


    //here I'd return the array loaded

  },

});

这是我的前端。

Meteor.call(
    'getSummary',0,Session.get('start_date'),Session.get('end_date'),1,
    function(error, result){
        if(error){
            console.log(error);
        } else {        
                Session.set('sumTotals',result);
        }
    }
    );

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    终于明白了。我使用了 wrapSync

     'getSummary': function (dept,startDate,endDate,filterType) {
             console.log(dept);
             console.log(startDate);
             console.log(endDate);
             console.log(filterType);
             var startdate = new Date(startDate);
             var enddate = new Date(endDate);
             var arr = [];
             f = myQuery(startdate,enddate,dayFinalGroup);   
    
             var fetchCursor = Meteor.wrapAsync(function fetchCursor (cursor, cb) {
    
                            cursor.each(function (err, doc) {
                            if (err) return cb(err);
                            if (!doc) return cb(null, { done: true }); // no more documents
    
                            arr.push(doc);
                            });
            });
    
    
         var myData = fetchCursor(f);
    
    
    
    
            return arr;  
    

    【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2023-03-15
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多