Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@OdongAlican
Created April 14, 2020 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OdongAlican/908708d9ad1479e1d6846c8cd06c60c9 to your computer and use it in GitHub Desktop.
Save OdongAlican/908708d9ad1479e1d6846c8cd06c60c9 to your computer and use it in GitHub Desktop.
Creating a post request for an article
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