Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. const PostSchema = new Schema({
  2. ...
  3. comments: [
  4. {
  5. user: {
  6. type: Schema.Types.ObjectId,
  7. ref: 'user',
  8. },
  9. body: {
  10. type: String,
  11. required: [true, 'Content required'],
  12. },
  13. }
  14. ],
  15. ...
  16. });
  17.  
  18. PostRouter.put('/posts/comments', (req, res) => {
  19. const { id } = req.query;
  20. const userID = req.body.user;
  21. const body = req.body.body;
  22. const comment = {
  23. user: mongoose.Types.ObjectId(userID),
  24. body: body,
  25. };
  26.  
  27. Posts
  28. .update({ _id: mongoose.Types.ObjectId(id) }, { $push: { comments: comment }})
  29. .then(result => {
  30. res.status(200).json(result.ok);
  31. })
  32. .catch(err => console.log(err));
  33. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement