如何在发布中将数组转换为游标



以下代码:

Meteor.push("svse",function(){   
   if(UserUtils.isAdmin(this.userId)) //is Administrator?
       return Svse.find();
   var arr = ["1","1.2"]; //just a example
   var nodes = Svse.find({sid:{$in:arr}}).fetch();
   var newNodes = new Array();
   for(i in nodes){
       var newNode = nodes[i];
       newNode["son"] = ArrayUtils.intersect(arr,newNode["son"]);
       newNodes.push(newNode)
    }
    return newNodes;
});
ArrayUtils={};
Object.defineProperty(ArrayUtils,"intersect",{
value : function(a,b){
    var ai=0;
    var bi=0;
    var result = new Array();
    while( ai < a.length && bi < b.length ){
        if(a[ai] < b[bi] ) {
            ai++;
        } else if(a[ai] > b[bi] ){
            bi++; 
        } else {
            result.push(a[ai]);
            ai++;
            bi++;
        }
    }
    return result;
}
});

在流星启动时导致错误:

 子 ac338EvWTi2tpLa7H 错误的异常:      发布函数返回了一个非游标数组

如何将数组转换为游标? 或者像ArrayUtils.intersect()查找查询中一样处理数组 在这里操作?

它认为 Meteor.push 是你第一行代码中的一个错别字。

发布函数需要返回集合游标或集合游标数组。 从文档中:

发布函数可以返回 Collection.Cursor,在这种情况下,Meteor 会将光标的文档发布到每个订阅的客户端。您还可以返回 Collection.Cursors 数组,在这种情况下,Meteor 将发布所有游标。

如果你想发布 newNodes 中的内容,并且不想在服务器端使用集合,那么在发布中使用this.added。 例如:

Meteor.publish("svse",function(){  
  var self = this;
  if(UserUtils.isAdmin(self.userId)) //is Administrator?
    return Svse.find();  // this would usually be done as a separate publish function
  var arr = ["1","1.2"]; //just a example
  Svse.find({sid:{$in:arr}}).forEach( function( newNode ){
    newNode["son"] = ArrayUtils.intersect(arr,newNode["son"]); //is this just repeating query criteria in the find?
    self.added( "Svse", newNode._id, newNode ); //Svse is the name of collection the data will be sent to on client
  });
  self.ready();
});
对我来说,要

遵循填充 newNode 的查找和相交函数所期望发生的事情有点困难。 您可能只需使用 find 来限制返回的字段,就可以执行相同的操作。

最新更新