0
私はポスト/コメントシステムを作っています。ここでは、ユーザーが投稿にコメントできるようにしたい(私の場合は「キャプチャ」と呼ばれます)。Mongodb |コメントをポピュレート投稿
私は現在、投稿の作成以外はすべて設定しています。
私は別々の文書にコメントを追加することができますし、私は彼らがrefrencedてしまったが、私は移入Methodeのを追加しようとすると、それは何もしません。
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);
ができます:
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);
});
});
});
};
キャプチャモデル:
これは私の完全なコードです["5744bafc1460f5bf18c17ac0", "5744bd4cfcd57403198f0987"]
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);