如何通过angularJs获取嵌套文档的ObjectId?

时间:2018-11-14 10:58:58

标签: angularjs

这是删除评论的代码。我可以获取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);

那么我如何获得commentId?谢谢您的帮助和时间!

0 个答案:

没有答案