DEV Community

Ozoemena
Ozoemena

Posted on

How i solved a bugπŸ˜€πŸ˜€πŸ˜€

I was working on a personal project that is very personal to me. I am building an app that will help business people record their transactions and provide security for them against employees stealing from them too. This project is special to me because it is focused on my Igbo people who happen to be mostly traders and uneducated. I wanted to create a delete button that would enable them to delete an entry. I noticed that when I make an entry and delete it before saving it at the backend, it works perfectly but when I want to delete an entry fetched from the backend, it throws an error. At first, I was thrown off as I did not expect it. I remembered the advice from my mentor, he said that whenever I have a bug, I should calm down, and investigate every step before calling for help. His reason is that that is the only way to grow faster. I followed that advice and discovered the problem.
At the button
state.map((item) => {
deleteHandler(item.id)}>... })
Now when I click on this button, it picks the particular item that is mapped and extracts the id for that item. The id is then taken as a parameter for the deleteHandler function which filters the array of objects and looks for the item with an id equal to that filtered out, It then renders the remaining that are not the same with the id.
I discovered that entries made at the frontend have this id and are easily deleted but the entries from the backend, that's where the problem is, it does not have an id key, but _id and so when the id is consoled gives me "undefined". Now the problem becomes, how to distinguish this that with a click, an entry will be deleted whether it's coming from the front or back.
After some thought, I had to come up with an idea, a conditional statement that checks to see if the item has an id or if the id is equal to "undefined". Instead of extracting the idof the entry when clicked, i decided to pick the whole entry so that the delte function will check if it has an id or if the id is equal to undefined.
state.map((item) => {
deleteHandler(item)}>... })
That is how I was able to solve that problem.
Looking back at the whole thing, I feel proud as it gives me that dopamine rush and a sense that am getting towards becoming the kind of programmer that I want to be, one that uses codes to solve problems. I do not want to just write codes, I want to solve problems faced by business people, farmers, hospitals, government and their agencies. I want to add value to human life.

Top comments (0)