2017-02-12 65 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