2017-02-19 9 views
1

私はMongoose/MongoDBを使用しています。私は1つの記事に多くのコメントを関連させようとしています。私のアプリはウェブサイトからスクラップして始まり、ユーザーはMongoDBにスクラップされた各記事を保存するオプションを持っています。ユーザーが1つの記事を保存することを選択すると、その記事をデータベースに保存します。ユーザーが保存した記事の1つをクリックすると、その記事にコメントを付けることができます。各記事には、正しいコメントを取得するために必要な独自のコメントセクションがあります。多くのコメントを1つの記事の関係に含める必要がありますMongoDB

// JSファイル内の私のポストのコメント要求

function postComment(){ 

    var articleComment = { 
     comment: $('#comment').val().trim() 
    } 

    $.post('/comments/' + articleID, articleComment).done(function(data){ 
     $('.main-popup').fadeOut(); 
     console.log('DONNE', data); 
    }); 
} 

コントローラで//ポストルート

router.post('/comments/:id', function(req, res){ 

    var newComment = new Comment(req.body); 

    newComment.save(function(err, doc){ 
     if(err){ 
      console.log(err); 
     }else{ 
      Comment.findOneAndUpdate({ "_id": doc._id }, { "article": req.params.id }).exec(function(err, doc){ 
       if(err){ 
        console.log(err); 
        res.send(err); 
       }else{ 
        res.send(doc); 
       } 
      }); 
     } 
    }); 

}); 

//特定の記事をクリックしたときに正しいのコメントを取得するための要求を取得し

function showCommentBox(){ 

    $('.comments').empty(); 
    $('#comment').val(""); 
    articleID = $(this).attr('data-article-id'); 

    $.get('/comments/' + articleID, function(data){ 


     if(data.article){ //This is undefined********************* 
      for(var x = 0; x < data.comment.length; x++){ 

       $('.comments').append("<div><h2>" + data.comment[x].comment + "</h2><span><button>&times;</button></span></div>"); 
      } 
     } 
     $('.main-popup').fadeIn(); 
    }); 


} 

//コントローラで経路を取得する

router.get('/comments/:id', function(req, res){ 
    Comment.findOne({ "article": req.params.id }).populate("article").exec(function(err, doc){ 

     if(err){ 
      console.log(err) 
     }else{ 
      res.json(doc); 
     } 
    }); 
}); 

//条モデル

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


var ArticleSchema = new Schema({ 

    title: { 
     type: String 
    }, 
    link: { 
     type: String 
    }, 
    description: { 
     type: String 
    }, 
    img: { 
     type: String 
    } 
}); 

var Article = mongoose.model("Article", ArticleSchema); 

module.exports = Article; 

//あなたが.findOneAndUpdateを行う際のモデル

var mongoose = require('mongoose'); 

var Schema = mongoose.Schema; 

var CommentSchema = new Schema({ 

    comment: { 
     type: String 
    }, 
    article: { 
     type: Schema.Types.ObjectId, 
     ref: 'Article' 
    } 

}); 


var Comment = mongoose.model('Comment', CommentSchema); 
module.exports = Comment; 

答えて

0

まずコメント、あなたは$セットを逃しています。また、私はそれを設定する前に文字列をMongo ObjectIdに変換する必要があると思います。 だから、このliktになります。また、あなたは2つのデータベース呼び出しを行う必要はありません

const ObjectId = mongoose.Types.ObjectId; 
 

 
Comment.findOneAndUpdate({ "_id": doc._id }, {$set: {"article": new ObjectId(req.params.id) }})

。あなたは、文書番号がnewCommentを保存する前にして、できただけで、このような応答として送信:

//Please notice that mongoose.Schema.Types.ObjectId and mongoose.Types.Object are different types. 
 
//You need this one here: 
 
const ObjectId = mongoose.Types.ObjectId; 
 

 
router.post('/comments/:id', function(req, res){ 
 
\t var newComment = new Comment(req.body); 
 
\t newComment.article = new ObjectId(req.params.id); 
 

 
\t newComment.save(function(err, doc){ 
 
\t \t if (err) { 
 
\t \t \t console.error(err); 
 
\t \t \t res.send(err); 
 

 
\t \t \t return; 
 
\t \t } 
 

 
\t \t res.send(doc); 
 
\t }); 
 
});

+0

さて、入力をお願いいたします。しかし、私はデータベースへの呼び出しが2回必要だと思います。ユーザーがコメントボタンをクリックすると、私のコメントのモーダルが表示されます。私のコメントモーダルの中のサブミットボタンは、ポストリクエストを作るものです。 – henhen

+0

@anonymous私は別のことについて話してきました。 POSTエンドポイント内で1ではなく2つのコールを行います。 1)。goo go to mongo 2).findOneInUpdateはmongoに行き、同じ文書を修正します。これは1回の呼び出しで行う必要があります。 –

関連する問題