Created
April 14, 2020 20:33
-
-
Save OdongAlican/908708d9ad1479e1d6846c8cd06c60c9 to your computer and use it in GitHub Desktop.
Creating a post request for an article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.post = async function (req, res) { | |
const authorId = await req.params.authorID; | |
const postObject = await req.body; | |
const newPost = new PostModel(postObject); | |
await AuthorModel.findOne({ _id: authorId }, async (err, foundAuthor) => { | |
if (!foundAuthor) { | |
return err; | |
} | |
foundAuthor.posts.push(newPost); | |
newPost.author = foundAuthor; | |
await newPost.save((error, savedPost) => { | |
if (error) { | |
return error; | |
} | |
return res.json(savedPost); | |
}); | |
await foundAuthor.save((error, savedAuthor) => { | |
if (error) { | |
return error; | |
} | |
return res.json(savedAuthor); | |
}); | |
return foundAuthor; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment