I am new to the whole mean stack. I am about to create a CRUD application. I already have the easy ones, C and R but now I want to go for D. Unfortunately, when I try to delete an entry I get a 500 error. My routes are getting called but after that nothing happens.
Html
<div ng-repeat="article in articles">
<form ng-click="deleteArticle(article)">
<button type="submit" class="btn btn-primary">Delete</button>
</form>
</div>
Angular
The part that is in my controller..
$scope.deleteArticle = function(article) {
articlesFactory.removeArticle(article) }
And the part that is getting called in a factory:
art.removeArticle = function(article) {
return $http.put('/articles/' + article._id + '/remove')
};
Routes
...
router.param('article', function(req, res, next, id) {
var query = Article.findById(id);
query.exec(function (err, article) {
if (err) { return next(err); }
if (!article) { return next(new Error("Unable to find this article.")); }
req.article = article;
return next();
});
});
...
router.put('/articles/:article/remove', function(req, res, next) {
console.log("I arrived in the routes")
req.article.remove(function(err, article) {
if (err) { return next(err); }
res.json(article);
});
});
...
Model
var ArticleSchema = new mongoose.Schema({
title: String,
content: String,
likes: { type: Number, default: 0 },
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
ArticleSchema.methods.remove = function(callback) {
this.remove(callback);