I also use this function in a different location. It works fine there but does not work in this section. It does not show any error on the console. What is the reason of this?
function getAllSearched() {
let users = Storage.getSearchedUsersFromStorage();
users.forEach(function (user) {
ui.addSearchedUserToUI(user)
})
}
addSearchedUserToUI(userName) {
let users = Storage.getSearchedUsersFromStorage();
if (users.indexOf(userName) === -1) {
const li = document.createElement("li")
li.className = "list-group-item"
li.textContent = userName;
this.lastUsers.appendChild(li)
}
}
I think you need to give us a link to your entire codebase. These are taken out of context and there really isn’t enough information to know why they aren’t working. Also, it might help if you explain exactly what you want to do and what it is/isn’t doing.
This is better, but I don’t think it is enough. Can you put the entire project (all files needed to run it) into github and give us a link to the repo so we can actually run your project locally? Also, you haven’t told us what exactly is broken and what is supposed to happen. But we really need to be able to see the entire thing in action.
Problem; The problem with these codes is that I have a function called addSearchedUserToUI in ui.js. I run this function in getData function in api.js and it works fine. But I cannot run this function in getAllSearched function in api.js where I added “DOMContentLoaded” event.
Actually, the ui.addSearchedUserToUI method is executed when DOMContentLoaded is triggered and getAllSearched is executed, it’s just not running the way you intended.
You’ve got some logic issues here. The method ui.addSearchedUserToUI is doing the following check before it adds a user to the list:
if (users.indexOf(userName) === -1) {
This means that it will only add the user to the list if their username is NOT in the users array. The users array is created by getting all of the usernames stored in local storage. So if a username is in local storage then it will not be added to the list. Do you see the logical error here?
The reason ui.addSearchedUserToUI works in getData is because you haven’t added the username to local storage before you call ui.addSearchedUserToUI. If you move the call to ui.addSearchedUserToUI after you add the username to local storage then you will run into the same problem as above.
I cannot see a logic error in this section. If there is that element in the list, I prevent it from being added again. There is no problem here. I understand why the function does not work, but I cannot see an error in the part you say there is a logic error.
I guess I don’t understand what you are trying to do here? When does the DOMContentLoaded event fire? When you load/reload the page. So you are calling getAllSearched when you load/reload the page. That function loads all the users saved in local storage and then tries to add them to the list using ui.addSearchedUserToUI. But ui.addSearchedUserToUI won’t add them to the list because of the logic error I pointed out above.
So from what I can tell, when the page is loaded/reloaded, you want to get all the users saved in local storage and add them to the list on the page. Is this really what you want to do?
Well, that would take all of the fun out of it for you
I think for starters it would be helpful if you renamed some of these functions/methods to make it easier to follow the logic. For example, ui.addSearchedUserToUI, what does “SearchedUser” actually mean? Why do you need to do those local storage checks? Why not just call it addUserToUI and get rid of all the local storage checks. Then you would be able to call addUserToUI from both getAllSearched and getData.
Please give it a proper code base. And please give me a link to the repo so we can actually run your project locally?. After that I will tell you the real issue
Since I want to create a list of the last users we searched for, I do this because the same name is not added to the list twice and these last searched people are not deleted when the page is refreshed. If you have run index.html in your local, this list is created under “son aramalar”. “son aralamar” means “Recent Searches” in English
Preventing duplicates is definitely something you want to do but I think it would be easier to follow logically if you didn’t include that logic in the function that adds the user to the list but rather have that as a separate function. Then you would first check to see if the user is already in the list before you call addUserToUI.
I see that you are already checking for duplicates when you add them to local storage, so you are never going to have a duplicate problem when you pull them from storage when the page is loaded. The duplicate problem is going to occur when you click the Ara button. That is when you want to check for duplicates using a function that specifically checks for duplicates. If it isn’t a duplicate, then you can add the user to the UI list and local storage.
Also, there may be some people who deny your app permission to use local storage, so you won’t always have it available. So you need to be able to check for duplicates when you can’t use local storage. This is why the duplicate check needs to be independent of local storage.