From f1a39bdc6bbb2182a8a048d1110ef4acd74e726a Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Fri, 25 Nov 2016 03:47:32 +0100 Subject: [PATCH] =?UTF-8?q?when=20changing=20author,=20remove=20article=20?= =?UTF-8?q?from=20previous=20author=E2=80=99s=20article=20array?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/articles.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/controllers/articles.js b/controllers/articles.js index efd48ee..54bde25 100644 --- a/controllers/articles.js +++ b/controllers/articles.js @@ -56,6 +56,10 @@ router.get('/:id/edit', function(req, res){ }); router.put('/:id', function(req, res){ + Author.findOne({'articles._id':req.params.id}, function(err, previousAuthor){ + previousAuthor.articles.id(req.params.id).remove(); + previousAuthor.save(); + }); Author.findById(req.body.authorId, function(err, foundAuthor){ Article.findByIdAndUpdate(req.params.id, req.body, {new:true}, function(err, updatedArticle){ foundAuthor.articles.push(updatedArticle);