Problem with reading information from array
Hello there,
I have the following problem:
This is my JS Code:
I can perfectly read the information out of the locales array when inside the event listener but when I try to console.log something from the array somewhere else in my code it suddenly does not work anymore.
Can anyone help me with that?
Greetings,
and thanks in advance. π
14 Replies
Are you sure that you are reading the value of locales at the correct time? It is an empty object before the event listener's callback is run. If you attempt to read the values before the event, it will still be an empty object.
The UI is only opened via the event listener.
"when I try to console.log"
You do this console log inside of UI code?
Afterwards I for example run this function:
function AddParkedCars(ownedCars) {
console.log(locales.Servername)
}
But then it prints: undefined
Yes
What do you get if you console log locales instead of locales.Servername?
[Object object] π
There is definitely something not right about that. The console is capable of displaying objects. The conversion to [Object object] normally happens if you try to set an object as the innerText or innerHTML and display it on a web page. It will also happen if you convert an object to string without using JSON.stringify(). Some other code is modifying the value.
I recommend changing the name of "locales". It is possible that you imported a library that uses the same name for something else.
Changed it to localizations but still it prints object object
But also when I let it be console.logged in the event listener itself it prints Object object
But when I then do something like localizations.Servername it works inside the event listener
As a test, comment out the entire section where the value of locales is assigned and temporarily replace it with:
See if the console log works with that.
Works everywhere.
The console log with that value is showing properly?
yes.
OK. Try assigning only the Servername part and comment out the rest. See if that works.
Prints undefined
wait..
Fixed it. You were right in the beginning.
...
Didn't see it at first.
Anyway thanks really much for the help - appreciate it β€οΈ