2013-10-29 2 views
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; 
} 
}); 

:カーソルに配列を変換する方法

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

?または検索配列のArrayUtils.intersect()のような配列を処理すると、hereが動作しますか?

答えて

6

Meteor.pushは、コードの最初の行でタイプミスです。

パブリッシュ関数は、コレクションカーソルまたはコレクションカーソルの配列を返す必要があります。 docsから:

流星がそれぞれ加入し、クライアントにそのカーソルのドキュメントを公開します。その場合には機能は、Collection.Cursorを返すことができます公開します。 Collection.Cursorsの配列を返すこともできます。この場合、Meteorはすべてのカーソルを公開します。

newNodesにあるものを公開し、サーバー側でコレクションを使用しない場合は、公開の内部にthis.addedを使用します。例えば:私はあなたがnewNodeを移入見つけると交差する機能の両方で起こることを期待ものをフォローするために

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

それは少し厳しいです。 findを使用して同じことを行うことができるかもしれませんが、fieldsが返されます。

+0

お時間をありがとうございます!今では私のコードも同様に動作します!ありがとうございました!! –

+0

興味深いことに、文字列の配列をパブリッシュするために追加されたメソッドを使用しようとしましたが、文字列 "predrag"をクライアントに置く代わりに、オブジェクト{0: "p"、1: " "r"、5: "a"、6: "g"、_id:0.39238154771737754}なぜか? –

関連する問題