In this episode, we tackle what happens when a user gets deleted from our app—the kind of thing that could definitely break the user experience if we’re not careful. We start by actually deleting a user (Fred, sorry Fred) from the database, just like it would happen from the frontend. When this happens, their posts stick around, but their userId
gets set to null. Suddenly, any code that expects a user object (like for showing their avatar or username) is going to run into trouble.
So, we dive into the relevant components. First, we update the post component so anywhere we’re using the user object (like displaying the avatar or the username), we use the null-safe operator. If the user’s gone, we just display “user deleted” as their username and skip showing the avatar completely. That keeps things much tidier—no more broken images or blank spots.
We quickly set up similar logic anywhere else in the app that references a (possibly deleted) user, including on the homepage where we show the latest post. The whole idea here is to gracefully handle missing user info and make it clear in the UI when someone’s account is gone, without losing the context of their posts or causing errors.
By the end, we’ve got a snappy little enhancement: the app won’t break if a user goes away, and other users can clearly see when an account has been deleted. This is an important bit of polish for any community-driven app!