social_media_app icon indicating copy to clipboard operation
social_media_app copied to clipboard

getting an internal server error on deleting a post.

Open ifuzail opened this issue 1 year ago • 3 comments

As far as I have debugged the code the problem lies in the postId and the imageId. it says that files are not passing to the function. plus I have added videoId for video posts and comments in the post schema. I'm passing those IDs as well but still not working.

here is the API function :

export async function deletePost(post) {

  if (!post?.postId || !post?.imageId || !post?.videoId ) {
    console.error('Invalid post data for deletion');
    return { status: 'error', message: 'Invalid post data for deletion' };
  }

  try {
    const statusCode = await databases.deleteDocument(
      appwriteConfig.databaseId,
      appwriteConfig.postCollectionId,
      post?.postId
    );

    if (!statusCode) throw Error;

      await deleteFile(post?.imageId)

      if(post?.videoId) {
        await deleteFile(post?.videoId)
      }

    return { status: "ok" };

  } catch (error) {
    console.log(error);
  }
}

this is the handleDelete function i m calling the post details:


const handleDeletePost = () => {
    try {
      const deletedPost = deletePost({
        post,
        postId: postId,
        imageId: post?.imageId,
        videoId: post?.videoId,
      });
  
      if (deletedPost && deletedPost.status === 'ok') {
        router.push('/');
      } else {
        console.error('Failed to delete post.');
        // Optionally, you can show an error message or handle the failure in another way.
      }
    } catch (error) {
      console.error('Error deleting post:', error);
      // Handle the error accordingly, e.g., show an error message to the user.
    }
  };

this is the mutation fn:


export const useDeletePost = () => {
  const queryClient  = useQueryClient();

  return useMutation({
    mutationFn: (post) => deletePost(post),
    onSuccess: (data) => {
      queryClient.invalidateQueries({
        queryKey: ['getRecentPosts', data.$id],
      });
    }
  })
}

additionally, I think that in appwrite, adrian set delete as null in other document, tell me if it will remain true for comments as well or i have to cahnge it to cascade.

ifuzail avatar Nov 25 '23 01:11 ifuzail

@ifuzail did you find a solution?

Onunkwor avatar Dec 18 '23 15:12 Onunkwor

Not sure if you got the same problem as me but when I was trying to delete post, if they were saved, then it wouldn't delete. While if they weren't saved, it would be deleted. So, what you've to do is, go to appwrite -> databases -> select your database -> Posts -> "Attributes" tab -> select the 3 dots on the right side of the "save" attribute -> click Edit and then scroll to "On deleting a document" and finally select "Cascade - delete all related documents" and click "Update". You should be able to delete posts now.

Matx99 avatar Mar 08 '24 18:03 Matx99

@Matx99 Thank you for the help but I gave up on the project but not completely. The problem was I finished the project but the app write kept messing up i had to delete the collections and set up the relationships each day because either a document didn't delete correctly which caused the app to lag or there was one problem or another. I've decided to learn Mongobd so i can redo the project but with my backend I've got the hang of it too.

Onunkwor avatar Mar 08 '24 19:03 Onunkwor