Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose')
- const allModules = require('../custom_modules/all-needed-modules')
- require('../models/Article')
- let Article = mongoose.model('Article')
- require('../models/Comment')
- let Comment = mongoose.model('Comment')
- let addComment = (req, res) => {
- // Method POST
- req.pathname = req.pathname || allModules.url.parse(req.url).pathname
- let urlId = req.pathname.split('/')[3]
- let body = ''
- req.on('data', (data) => {
- body += data
- })
- req.on('end', () => {
- let parsedBody = allModules.query.parse(body)
- let comment = new Comment({
- articleID: urlId,
- content: parsedBody.comment,
- date: Date.now()
- })
- Article
- .findByIdAndUpdate(urlId)
- .then(article => {
- article.comments.push(comment)
- article.save()
- }).then(() => {
- comment
- .save()
- .catch(err => console.log(err))
- })
- Article
- .find({_id: urlId})
- .then(articles => {
- console.log(articles)
- for (let article of articles) {
- Comment
- .find({articleID: article._id})
- .then((article) => {
- allModules.fs.readFile('./views/image-details.html', (err, data) => {
- if (err) allModules.errorHandler.throwError(err)
- res.writeHead(200, {'Content-Type': 'text/html'})
- data = data.toString()
- console.log(article)
- res.write(allModules.mustache.render(data, {Comments: article}))
- res.end()
- return
- })
- })
- }
- })
- .catch(err => console.log(err))
- })
- }
- module.exports = addComment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement