ObjectId 를 객체로 치환해주는 것
const {Schema} = mongoose;
const {Types : {ObjectId}} = Schema;
const commentSchema = new Schema({
commenter : {
type : ObjectId,
required : true,
ref : 'User',
},
...
router.post('/', async (req, res, next) => {
try{
const comment = await Comment.create({
commenter : req.body.id,
comment : req.body.comment,
});
console.log(comment);
const result = await Comment.populate(comment, {path : 'commenter'});
res.status(201).json(result);
}catch(err){
console.error(err);
next(err);
}
});
Comment.populate(comment, {path : 'commenter'})
: comment 의 'commenter' path 의 ObjectId 를 Object 로 치환하게 된다.