2017-02-12 22 views
1

我想获取任何已经在分贝中的文章的对象ID,以便我可以验证文章是否存在之前进行评论。如何获取对象ID以及如何更新

问题出在路由器上(/ blog/article/comment)。我无法从/ blog/article /:postid获取文章对象ID。我想通过此ID条款ArticleID这样的:

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

文章型号:article.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('/') 
     } 
     }); 
    } 
    ]); 

}); 

另一个问题我是如何更新commentId在各条评论

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

对于第二个问题,你想了一堆评论的ID添加到文章?这就是你要求的待办事项吗? – matt

+0

是的,我想每个评论ID保存到 – CODI

+0

评论的文章请参阅我的答案添加我添加。它应该让你走向正确的开始。 :) – matt

回答

1

由于/blog/article/comment路线是发布请求。只需在请求的正文中提交您的articleId即可。你必须从客户端发送它。你可以用req.body.articleID来访问它(如果这就是你所说的变量)。

有关节点中POST请求的更多信息,请参阅here

关于第二个问题:

在你的文章的架构,你有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('/') 
    } 
    }); 
    }); 
} 
... 

我没有完全实现的代码,但它应该让你起飞的权利开始。

addToSet Documentation Some more examples of add to set