2017-02-12 15 views
1

コメントが作成される前にアーティクルが存在することを検証できるように、すでにdbにあるアーティクルのオブジェクトIDを取得しようとしています。オブジェクトidの取得方法と更新方法

問題はルータ(/ blog/article/comment)にあります。記事のオブジェクトIDは/ blog/article /:postidから取得できません。私はこのような情報でarticleIDにこのIDを渡したい:

articleId: req.params.postid 

私も試してみました:

articleId: req.article._id 

モデル構造:comment.js

var mongoose = require('mongoose'); 

var CommentSchema = new mongoose.Schema({ 
    content: { type: String }, 
    user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, 
    articleId: { type: mongoose.Schema.Types.ObjectId, ref:'Article' }, 
    dateCommented: { type: Date, default : Date.now } 
}); 

記事モデル:記事.js

var ArticleSchema = new mongoose.Schema({ 
    category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' }, 
    commentId:{type: mongoose.Schema.Types.ObjectId, ref:'Comment'}, 
    title: String, 
    author: { type: mongoose.Schema.Types.ObjectId, ref: 'User'}, 
    blog: [{ 
    topic: { type: String, unique: false, lowercase: true }, 
    body: { type: String, unique: false, lowercase: true }, 
    tags: [ 'first', 'mongodb', 'express'], 
    created: Date, 
    modified: { type : Date, default : Date.now }, 
    state: { type: String, unique: false, lowercase: true } 
    }] 
}); 

main.js

router.param('postid', function(req, res, next, id) { 
    if (id.length !=24) return next(new Error ('The post id is not having the correct length')); 
    //articleId: req.param('postid'), 
    Article.findOne({ _id: ObjectId(id)}, function(err, article) { 
    if (err) return next(new Error('Make sure you provided correct post id')); 
    req.article = article; 
    next(); 
    }); 
}); 

router.get('/blog/article/:postid', function (req, res, next) { 
    Article.findById({ _id: req.params.postid }, function (err, article) { 
    if (err) return next(err); 
    res.render('main/publishedArticle', { 
     article: article 
    }); 
    }); 
}); 


router.post('/blog/article/comment', function(req, res, next) { 
    async.waterfall([ 
    function(callback) { 
     var comment = new Comment({ 
     articleId: req.params.postid, 
     content: req.body.content, 
     user: req.user._id 
     }); 

     comment.save(function(err) { 
      if (err) return next (err); 
      req.flash('success', 'Thank you for your comment'); 
      callback(err, comment); 
     }); 
    }, 
    function(comment) { 
     Article.update({_id : comment.articleId }, { $set: { commentId: {} }}, function(err, updated) { 
     if (updated) { 
      res.redirect('/') 
     } 
     }); 
    } 
    ]); 

}); 

私が持っているもう一つの問題は、/blog/article/comment以来条

Article.update({_id : comment.articleId }, { $set: { commentId: {} }}, function(err, updated) 
+0

、あなたは記事にコメントIDの束を追加したいですか?それはあなたのやり方を尋ねるものですか? – matt

+0

はい、私は各コ​​メントidをコメントに書かれた記事に保存したいです – CODI

+0

私が追加した追加については私の答えを見てください。それはあなたを正しいスタートに追いやるはずです。 :) – matt

答えて

1

に各コメントcommentIdを更新する方法でありますルートは投稿要求です。その要求の本文に記事IDを送信するだけです。あなたはそれをクライアントから送信しなければなりません。 req.body.articleID(これが変数と呼ばれる場合)でアクセスできます。

nodeのPOST要求の詳細については、hereを参照してください。あなたの2番目の質問については

あなたがcommentIdているあなたの記事スキーマ内では、それは単一のレコードです。あなたが望むのは、一連のコメントです。このような何か:

comments: [{type: mongoose.Schema.Types.ObjectId, ref:'Comment'}] 

次に、あなたのコード内...

... 
function(comment) { 
    //comment should contain all the comments 

    //Grab the article 
    Article.findOne({ _id: comment.articleId}, function(err, article){ 

    //Go through all the comments in 'comment' compare them with the ones in artcle.comments. 
     //The ones that aren't already in the article object get put into newComments... 
    var newComments = []; 

    Article.update({ _id: comment.articleId }, { $addToSet: { comments: newComments } }, function(err, updated) { 
    if (updated) { 
     res.redirect('/') 
    } 
    }); 
    }); 
} 
... 

私は完全にコードを実装していませんでしたが、それは右のスタートにあなたをオフに取得する必要があります。 2番目の質問については

addToSet Documentation Some more examples of add to set

関連する問題