2017-12-28 63 views
1

私は完全にMongooseで立ち往生し、メソッドを削除します。 コメントとフォームのあるページには、ボタンDeleteがあります。私の目標は、クリックされたコメントだけを削除することです。以下は私のMongoDBファイルです(ちなみに、expressライブラリのoverrideメソッドを使ってリクエスト投稿と削除の両方を処理しています)。Mongo内の配列から要素を取り除く方法



{ 
    "_id": { 
     "$oid": "5a455cf460414f548f3d1afb" 
    }, 
    "title": "Tets", 
    "body": "tes", 
    "user": { 
     "$oid": "5a440bae124b7e4626aeeb70" 
    }, 
    "date": { 
     "$date": "2017-12-28T21:07:00.194Z" 
    }, 
    "comments": [ 
     { 
      "commentBody": "ets", 
      "commentUser": { 
       "$oid": "5a440bae124b7e4626aeeb70" 
      }, 
      "_id": { 
       "$oid": "5a455cf660414f548f3d1afc" 
      }, 
      "commentDate": { 
       "$date": "2017-12-28T21:07:02.143Z" 
      } 
     } 
    ], 
    "allowComments": true, 
    "status": "public", 
    "__v": 1 
} 

私のスキーマ



     const mongoose = require('mongoose') 
     const Schema = mongoose.Schema; 

     //Create Schema 
     const StorySchema = new Schema({ 
     title: { 
      type: String, 
      required: true 
     }, 
     body: { 
      type: String, 
      required: true 
     }, 
     status: { 
      type: String, 
      default: 'public' 
     }, 
     allowComments: { 
      type: Boolean, 
      default: true 
     }, 
     comments: [{ 
      commentBody: { 
       type: String, 
       required: true 
      }, 
      commentDate: { 
       type: Date, 
       default: Date.now 
      }, 
      commentUser: { 
       type: Schema.Types.ObjectId, 
       ref: 'users' 
      } 
     }], 
     user: { 
      type: Schema.Types.ObjectId, 
      ref: 'users' 
     }, 
     date: { 
      type: Date, 
      default: Date.now 
     } 
    }); 

     mongoose.model('stories',StorySchema, 'stories'); 

そして、私のJSファイル、私のポストの方法は、私が望む正確にどのように動作しますが、すべてでは動作しません削除(未定義のプロパティ「コメント」を読み込めません)

ここ


    router.post('/comment/:id' , (req , res) => { 
    Story.findOne({ 
     _id: req.params.id 
    }) 
    .then(story => { 
     const newComment = { 
      commentBody: req.body.commentBody, 
      commentUser: req.user.id 
     } 

     //Push to comments array 
     story.comments.unshift(newComment); 

     story.save() 
     .then(story => { 
      res.redirect(`/stories/show/${story.id}`) 
     }) 
    }); 
    }) 

    router.delete('/comment/:id', (req, res) => { 
    Story.remove({ 
     _id: req.body.id.comments 
    }) 
    .then(() => { 
     req.flash('success_msg', 'Comments Removed!'); 
     res.redirect('/dashboard'); 
    }) 
    }); 

は、私のハンドルは、フォームにファイルされて

{{#each story.comments}}

<form action="/stories/comment/{{id}}?_method=DELETE" method="post" id="delete-form"> 
<input type="hidden" name="_method" value="DELETE"> 
<button type="submit" class="btn red"><i class="fa fa-remove"></i> Delete</button> 
</form> 

{{/each}} 

エラーが私は

TypeError: Cannot read property 'comments' of undefined 
at router.delete (/Users/ar2z/Desktop/fierce-caverns-70427/routes/stories.js:197:20) 

が私を助けてくださいました。私は完全に失われています。

+1

送信したリクエストを表示してください。このエラーは、あなたのリクエストボディが "id"オブジェクトを持っていないというヒントを与えます – Christian

答えて

0

最近、実際に同じエラーが発生しました。あなたがこれを行う必要があり、私を発見した:

router.delete("/comments/:id", function(req,res){ 
    var Model = require("myModel"); 
    //Run a find and update query to delete the comment 
    Model.findOne({_id:req.params.id}, function(err,doc){ 
     if(doc && !err){ 
     doc.comments = doc.comments.filter(function(comment){ 
      //This will filter out the comment you want to delete 
      return comment._id != req.body.commentId 
     }) 
     } 
     res.redirect("/comments/") 
    }) 
} 
+0

あまりにも助けてくれてありがとう、残念なことに、ここで "doc"が意味するところで私にとってはうまくいきませんか? –

+0

'' doc'は 'findOne'メソッドで見つかった文書です。その後、フィールドにアクセスしてフィールドを変更し、最後に変更されたコピーを保存することができます。 – Andy

+0

ありがとう、アンディ、私はそれを得た –

0

私の障害は、私は配列要素で仕事をしようとしていた当初から上記



    Story.remove({ 
      _id: req.body.id.comments 
     }) 

コードIはMongoのオブジェクトに働いているのと同じ方法でしたそれは、配列から削除要素オブジェクトではなくのために働いている配列要素のために働いていない私が使用します。



Story.update({ }, { $pull: { comments: { _id: req.params.id }}}, { multi: true }) 

このコードはドキュメント

の配列から項目を削除します

$pull MongoDb documentation

関連する問題