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

L.T*_*L.T 5 meteor

以下代码:

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;
}
});
Run Code Online (Sandbox Code Playgroud)

在流星启动时导致错误:

 Exception from sub ac338EvWTi2tpLa7H Error: 
      Publish function returned an array of non-Cursors

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

use*_*291 6

它认为Meteor.push是你第一行代码中的拼写错误.

发布函数需要返回Collection游标或Collection游标数组.来自docs:

发布函数可以返回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();
});
Run Code Online (Sandbox Code Playgroud)

使用填充newNode的find和intersect函数来跟踪你期望发生的事情有点困难.您可以使用find来限制返回的字段.