2016-05-17 12 views
2

私はMeteorJsを初めて使いました。私はそれを愛しています。しかし、私は、意見をもって(イエスかノー)、それに投票しなければならないmeteorJsアプリケーションに取り組んでいます。構造は、質問(メインポスト)がコメントの親であり、コメントが親であるようなものです。>質問(オブジェクト)>はいまたはいいえ(配列)> likes(配列) この場合は質問)インデックスのコレクション内のネストされた配列を更新する

{ 
    "_id" : "s2QBCnv6fXv5YbjAP", 
    "question" : "Is this real change?", 
    "createdAt" : ISODate("2016-05-13T21:05:23.381Z"), 
    "yes" : [ 
      { 
        "heading" : "Yes It is", 
        "body" : "I think this government knows what they are doing. That's why there has not been any form of protest", 
        "email" : "I think this government knows what they are doing. That's why there has not been any form of protest", 
        "createdAt" : ISODate("2016-05-13T21:08:25.119Z"), 
        "replies" : [ ], 
        "likes" : [ 
          "sdfsd6sd556shsdbdjs88s", 
          "sdfsd6sd556shsdbdjkhj88s", 
          "the_use_id", 
          "the_use_id", 
          "the_use_id" 
        ] 
      }, 
      { 
        "heading" : "Well, Yes", 
        "body" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time", 
        "email" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time", 
        "createdAt" : ISODate("2016-05-13T21:10:47.123Z"), 
        "replies" : [ ], 
        "likes" : [ ] 
      } 
    ], 
    "no" : [ 
      { 
        "heading" : "Not at All", 
        "body" : "This is not the change I wanted. This is waste of four years and I amm waiting to see the promised change", 
        "email" : "[email protected]", 
        "createdAt" : ISODate("2016-05-13T21:12:58.977Z"), 
        "replies" : [ ], 
        "likes" : [ ] 
      } 
    ], 
    "author" : "admin", 
    "image" : "/cfs/files/QuestionImages/DzdpK6NdurZMTwAse" 

}

は何私がしたいことはこれです:好きな配列にLOGGEDINユーザーのIDをプッシュするヘルパーを書くようなボタンは、私はこのヘルパーを書いクリックされたときに

"click #like_yes_comment": function(event){ 
    event.preventDefault(); 

    var questionId = Template.parentData(1)._id; 
    Questions.update(
     {_id: questionId, , 'yes.heading':this.heading}, 
     {$push: {'yes.0.likes': "the_use_id"}} 
    );   

} 

このエラーが発生しました

Uncaught Error: Not permitted. Untrusted code may only update documents by ID. [403] 

その後、私はそれがこの

"click #like_yes_comment": function(event){ 
    event.preventDefault(); 

    var questionId = Template.parentData(1)._id; 
    Questions.update(
     {_id: questionId}, 
     {$push: {'yes.0.likes': "the_use_id"}} 
    );   

} 

のように見えるように再編集した後、それは動作しますが、それは唯一の配列「のような」最初に更新されます。

ここに私の質問があります。質問>はい/いいえ>好きな配列のようにそれぞれをどのように更新できますか?おかげ

答えて

0

は、私はあなたがあなたのはい配列

"yes" : [ 
     { 
      id : Random.id(), 
      heading : "..." 
      ... 

に余分なIDを生成し、位置$演算子を使うべきだと思うので:

Questions.update(
    {_id: questionId, "yes.id": yesId}, 
    {$push: {'yes.$.likes': userId}} 
); 
+0

彼女はすでにとして 'heading'を使用していますid。 @kenshinman - 0の代わりに$を使うと助けになるかもしれません。 –

+0

これはほとんど動作しますが、このエラーはコンソールに表示されます** allow-deny.js:483 Uncaught Error:許可されません。信用できないコードは、IDで文書を更新するだけです。 [403] **どのように私はMeteor.methodを使ってこれを解決できますか – Kenshinman

+0

あなたはその文書の_idを使う必要があります: 'var docid = Questions.findOne({_ id:questionId); Questions.update( {_id:docid._id、 "yes.id":yesId}、 {$ yes:$。likees:userId}} ); – Gemmi

関連する問題