如果帖子里面没有评论就删帖

问题描述

如果里面没有评论,我想删除一个帖子。这实际上有效,但如果帖子被删除,我不会收到返回消息。服务器响应为:"errors": [{ "message": "Cannot return null for non-nullable field Post.id.",..

如何在没有错误消息的情况下执行此操作,或者删除没有评论的帖子的更好方法是什么?

async deleteComment(_,{ postId,commentId },context) {
        const { username } = checkAuth(context);

        const post = await Post.findById(postId);
        if (post) {
            const commentIndex = post.comments.findindex(
                (c) => c.id === commentId
            );

            if (post.comments[commentIndex].username === username) {
                post.comments.splice(commentIndex,1); 
                await post.save();
                if (post.comments.length < 1) {
                    console.log("No comments available";
                    await post.delete();
                    return "Successfully deleted post"; // I want to get the message
                }
                return post;
            } else {
                throw new AuthenticationError("Action not allowed");
            }
        } else {
            throw new UserInputError("Post not found");
        }
    },

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)