I am using nodejs, express, mongo and coffeescript and I have a simple blog post with comments, and I would like to add the possibility to delete a specific comment at any given time. The schema looks like this:
Schema = mongoose.Schema
ArticleSchema = new Schema
title:
type: String
trim: true
required: true
body:
type: String
required: true
createdAt:
type: Date
default: Date.now
comments: [
body:
type: String
default : ''
user:
type: Schema.ObjectId
ref: 'User'
required: true
createdAt:
type: Date
default: Date.now
]
Routes for the article are mapped like this:
articles = require '../app/controllers/articles'
app.get '/', articles.index
app.get '/articles', articles.manage
app.get '/articles/new', auth.requiresLogin, articles.new
app.get '/articles/:articleId', articles.show
app.get '/articles/:articleId/edit', auth.requiresLogin, articles.edit
app.param 'articleId', articles.article
But how do I do something like this to be able to delete a comment?
app.get '/articles/:articleId/comment/:commentId/delete', auth.requiresLogin, articles.edit
If you mean how you should implement removal of a comment: you first retrieve the article document using articleId, and then you can find and remove the sub-document:
// find article
...
// find the comment by id, and remove it:
article.comments.id(commentId).remove();
// since it's a sub-document of article, you need
// to save the article back to the database to
// 'finalize' the removal of the comment:
article.save(function(err) { ... });