Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- deletePost: (req, res) => {
- "use strict";
- let id = req.params.id;
- User.findOneAndRemove({_id: id}).then(user => {
- user.prepareDelete();
- res.redirect('/admin/user/all');
- })
- }
- ----------------
- //user.prepareDelete
- prepareDelete: function () {
- let Comment = mongoose.model('Comment')
- for (let comment of this.comments) {
- Comment.findById(comment).then(comment => {
- comment.prepareDelete()
- comment.remove()
- })
- }
- let Problems = mongoose.model('Problems')
- for (let problem of this.problems) {
- Problems.findById(problem).then(problem => {
- "use strict";
- problem.prepareDelete()
- problem.remove()
- })
- }
- }
- ---------------
- //comment.prepareDelete
- prepareDelete: function () {
- let User = mongoose.model('User')
- User.findById(this.author).then(user => {
- "use strict";
- if (user) {
- user.comments.remove(this.id)
- user.save()
- }
- })
- let Problems = mongoose.model('Problems')
- Problems.findById(this.target).then(problem => {
- "use strict";
- if (problem) {
- problem.comments.remove(this.id)
- problem.save()
- }
- })
- }
- -------------------
- //problem.prepareDelete
- prepareDelete: function () {
- let User = mongoose.model('User')
- User.findById(this.author).then(user => {
- "use strict";
- if (user) {
- user.problems.remove(this.id)
- user.save()
- }
- })
- let Comment = mongoose.model('Comment')
- for (let commentId of this.comments) {
- Comment.findOneAndRemove(commentId).populate('author').then(comment => {
- "use strict";
- if (comment) {
- let author = comment.author
- let index = author.comments.indexOf(commentId)
- let count = 1
- author.comments.splice(index, count);
- author.save()
- comment.save()
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement