我正在建立一个帖子/评论系统,我希望用户能够对帖子发表评论(在我的案例中称为'捕获')。
我目前已经完成了所有设置,除了填充帖子。
我可以在一个单独的文档上添加注释,并且我对它们进行了修改,但是当我尝试添加填充方法时,它没有做任何事情:
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);
});
});
执行此操作时,它不会给出任何错误,但它也不会填充。 我只是得到了评论的ID,但它并没有填充“捕获”。
console.log($scope.capture.comments);
给出:
["5744bafc1460f5bf18c17ac0", "5744bd4cfcd57403198f0987"]
这是我的完整代码:
capture.js route:
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);
答案 0 :(得分: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);
});
});