obs-portal/models/Comment.js

23 lines
540 B
JavaScript
Raw Normal View History

2020-11-20 10:02:30 +00:00
const mongoose = require('mongoose');
2020-11-20 10:02:30 +00:00
const CommentSchema = new mongoose.Schema(
{
body: String,
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
article: { type: mongoose.Schema.Types.ObjectId, ref: 'Article' },
},
{ timestamps: true },
);
// Requires population of author
2020-11-20 10:02:30 +00:00
CommentSchema.methods.toJSONFor = function (user) {
return {
id: this._id,
body: this.body,
createdAt: this.createdAt,
2020-11-20 10:02:30 +00:00
author: this.author.toProfileJSONFor(user),
};
};
mongoose.model('Comment', CommentSchema);