adrianhajdin / social_media_app

Build a modern social app with a stunning UI with a native mobile feel, a special tech stack, an infinite scroll feature, and amazing performance using React JS, Appwrite, TypeScript, and more.
https://jsmastery.pro
1.06k stars 310 forks source link

getting an internal server error on deleting a post. #21

Open ifuzail opened 7 months ago

ifuzail commented 7 months ago

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.

Onunkwor commented 6 months ago

@ifuzail did you find a solution?

Matx99 commented 3 months ago

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.

Onunkwor commented 3 months ago

@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.