Как получить ObjectId вложенных документов через angularJs? - PullRequest
0 голосов
/ 14 ноября 2018

Это код для удаления комментария. Я могу получить postId, но var commentId = $routeParams.commentId; не дает мне objectId комментария.

vm.deleteComment = function () {
        var postId = $routeParams.id;
        var commentId = $routeParams.commentId;
        // var postId = $route.params.id;
        // var commentId = $route.params.commentId;
        postFactory.deleteComment(postId, commentId).then(function(response){
            if (response.status === 200) {
                $route.reload();
                toastr["success"]("Comment Deleted!");
            }
        }).catch(function (error) {
            console.log(error);
            toastr["error"]("unsuccessful.Please try again!");
        });
    }

Это фабрика для удаления

function deleteComment(postId, commentId){
        return $http.delete('http://localhost:8080/api/posts/' + postId + '/comments/' + commentId).then(complete).catch(failed);
    }

это мой экспресс код для удаления комментария

module.exports.deleteOneComment = function (req, res) {
    var commentId = req.params.commentId;
    Post
        .findByIdAndRemove(commentId)
        .select('comments')
        .exec(function(err, result){
            if (err ){
                console.log('Error while removing the commment');
                res.status(403).json({ "message": "Error while removing your comment!" });
            } else {
                res.status(200).json({ "message": "Comment deleted" });
                console.log('Your comment is Deleted!');
            }
        });
};

и это экспресс-маршрут

router
    .route('/posts/:postId/comments/:commentId')
    .delete(commentController.deleteOneComment);

это моя схема

var mongoose =  require('mongoose');

var commentSchema = new mongoose.Schema({
    comment: {
        type: String,
        required: true
    },
    commentOwner: {
        type: String,
        required: true
    }
});
var postSchema = new mongoose.Schema({
    title: {
        type: String
    },
    subtitle: {
        type: String
    },
    body: {
        type: String
    },
    comments: [commentSchema],
    poster : {
        type: String,
        unique: false,
        default: 'Biruk'
    }
});

mongoose.model('Post', postSchema);

так как я могу получить комментарий? Спасибо за вашу помощь и время!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...