如何解决如果帖子里面没有评论就删帖
如果里面没有评论,我想删除一个帖子。这实际上有效,但如果帖子被删除,我不会收到返回消息。服务器响应为:"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 举报,一经查实,本站将立刻删除。