1

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);
ZedsWhatSheSaid
  • 467
  • 2
  • 9
  • 23

2 Answers2

1

Should use the delete method of HTTP. And try this logic, it's from angular-fullstack controller templates (I'd recommend using it over creating your own endpoints, here's the link):

router.delete('/articles/:id/remove', function(req, res, next) {
  Article.findById(req.params.id, function (err, article) {
    if(err) { return next(err); }
    if(!article) { return res.send(404); }
    article.remove(function(err) {
      if(err) { return handleError(res, err); }
      return res.send(204);
    });
  });
});

You'll need to update this as well to use delete:

art.removeArticle = function(article) {
  return $http.delete('/articles/' + article._id + '/remove')
};
TommyMac
  • 299
  • 1
  • 6
  • Everything I built still works but when I click delete I receive this error now `ArticleSchema.methods.remove = function(callback) { ^ RangeError: Maximum call stack size exceeded npm ERR! weird error 8 npm WARN This failure might be due to the use of legacy binary "node" npm WARN For further explanations, please read /usr/share/doc/nodejs/README.Debian npm ERR! not ok code 0` – ZedsWhatSheSaid Jan 09 '15 at 08:30
0

I know this is an old post, but I stumbled upon it as i had the same problem, so maybe it can be useful for someone.

After trying your code and using some things of TommyMac's answer, this was my outcome and should help:

Controller: Your controller part was ok:

$scope.deleteArticle = function(article) {
  articlesFactory.removeArticle(article) }

Factory: You should use DELETE instead of PUT
It worked for me with the following function:

router.delete('/articles/:article/remove', function(req, res, next) {
  req.article.remove(function(err) {
    if (err) {
      return next(err);
    }
    return res.sendStatus(204);
  });
});

Model: for me, it worked without providing a "remove" function at the model (apparrently there is a default function), so remove the following snippet from your code:

ArticleSchema.methods.remove = function(callback) {
  this.remove(callback);
areiterer
  • 33
  • 1
  • 7