5

以下代码:

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

在流星启动时导致错误:

来自 sub ac338EvWTi2tpLa7H 错误的异常:
      发布函数返回一个非游标数组

如何将数组转换为游标?还是像ArrayUtils.intersect()此处操作查找查询一样处理数组?

4

1 回答 1

6

它认为 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 和 intersect 函数来遵循您期望发生的事情对我来说有点困难。您可能只需使用 find 并限制返回的字段即可执行相同操作。

于 2013-10-29T04:30:09.443 回答