2016-05-24 57 views
0

我正在发布帖子/评论系统,我希望用户能够对帖子发表评论(在我的案例中称为“抓图”)。Mongodb |用评论填充帖子

我目前有一切设置,除了填充的职位。

我可以在一个单独的文件添加注释,我得到了他们refrenced,但是当我尝试添加填入梅索德,它不会做任何事情:

router.get('/captures/:capture', function(req, res){ 
     Capture.findOne({_id: req.params.capture}, function(err, data){ 
      if(err) 
       throw err; 
      req.capture.populate('comments'); 
      res.json(data); 
     }); 
    }); 

在这样做时,它不给出任何错误,但它也不会填充。 我只是得到评论的编号,但它并没有填充'捕获'。

console.log($scope.capture.comments); 

给出:

["5744bafc1460f5bf18c17ac0", "5744bd4cfcd57403198f0987"] 

这是我的全码:

capture.js路线:

var Capture = require('../models/capture'); 
var Comment = require('../models/comment'); 

module.exports = function(router) { 
    router.post('/captures', function(req, res){ 
     var capture = new Capture(); 
     capture.birdname = req.body.birdname; 
     capture.place = req.body.place; 
     capture.userId = req.body.userId; 
     capture.author = req.body.author; 
     capture.picture = req.body.picture; 
     capture.created_at = new Date(); 


     capture.save(function(err, data){ 
      if(err) 
       throw err; 
      console.log(req.body); 
      res.json(data); 
     }); 
    }); 

    // Map logic to route parameter 'capture' 
    router.param('capture', function(req, res, next, id) { 
     var query = Capture.findById(id); 

     query.exec(function (err, capture) { 
      if (err) { return next(err); } 
      if (!capture) { return next(new Error("can't find post")); } 

      req.capture = capture; 
      return next(); 
     }); 
    }); 
    // Map logic to route parameter 'comment' 
    router.param('comment', function (req, res, next, id) { 
     var query = Comment.findById(id); 

     query.exec(function (err, comment) { 
      if (err) { return next(err); } 
      if (!comment) { return next(new Error("can't find comment")); } 

      req.comment = comment; 
      return next(); 
     }); 
    }); 

    router.get('/captures/:capture', function(req, res){ 
     Capture.findOne({_id: req.params.capture}, function(err, data){ 
      if(err) 
       throw err; 

==========> As you can see here, I'm trying to populate the given id 
==========> with the comments. Currently when I console log on my capture, 
==========> it only gives back the id, but I want it to be populated with 
==========> the corresponding comments. 

      req.capture.populate('comments'); 
      res.json(data); 
    }); 
}); 

    router.post('/captures/:capture/comments', function(req, res, next){ 
     var comment = new Comment(); 
     comment.body = req.body.body; 
     comment.userId = req.body.userId; 
     comment.author = req.body.author; 
     comment.created_at = new Date(); 
     comment.capture = req.capture; 

     comment.save(function(err, comment) { 
      if (err) { return next(err); } 

      req.capture.comments.push(comment); 
      req.capture.save(function(err, capture) { 
       if (err) { return next(err); } 

       res.json(comment); 
      }); 
     }); 
    }); 
}; 

拍摄模式:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var captureSchema = Schema({ 
    birdname: {type: String, required: true}, 
    place: String, 
    userId: String, 
    author: String, 
    picture: Schema.Types.Mixed, 
    created_at: Date, 
    comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment'}] 
}); 

module.exports = mongoose.model('Capture', captureSchema); 

评论型号:

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var commentSchema = Schema({ 
    body: String, 
    userId: String, 
    author: String, 
    created_at: Date, 
    capture: [{ type: Schema.Types.ObjectId, ref: 'Capture'}] 
}); 

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

回答

1

没有必要为此:

Capture.findOne({_id: req.params.capture}, function(err, data) 

只需做到以下几点,它应该工作:

router.get('/captures/:capture', function(req, res) { 
     req.capture.populate('comments', function (err, capture) { 
     res.json(capture); 
    }); 
});