Problem with reading information from array

Hello there, I have the following problem: This is my JS Code:
var locales = {};

window.addEventListener("message", function(event) {
let data = event.data;
switch (data.type) {
case "open":
locales = {
Servername: data.locales[0],
GarageTitle: data.locales[1],
ImpoundTitle: data.locales[2],
ParkedIn: data.locales[3],
ParkedOut: data.locales[4],
Impounded: data.locales[5],
ParkIn: data.locales[6],
ParkOut: data.locales[7],
ParkOutImpound: data.locales[8],
SearchCarHeading: data.locales[9],
SearchCarDescription: data.locales[10],
SearchPlaceholder: data.locales[11]
};

$(".server-name").text(locales.Servername);

break;
}
});
var locales = {};

window.addEventListener("message", function(event) {
let data = event.data;
switch (data.type) {
case "open":
locales = {
Servername: data.locales[0],
GarageTitle: data.locales[1],
ImpoundTitle: data.locales[2],
ParkedIn: data.locales[3],
ParkedOut: data.locales[4],
Impounded: data.locales[5],
ParkIn: data.locales[6],
ParkOut: data.locales[7],
ParkOutImpound: data.locales[8],
SearchCarHeading: data.locales[9],
SearchCarDescription: data.locales[10],
SearchPlaceholder: data.locales[11]
};

$(".server-name").text(locales.Servername);

break;
}
});
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
Chooβ™šπ•‚π•šπ•Ÿπ•˜
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.
GamerPro
GamerProOPβ€’13mo ago
The UI is only opened via the event listener.
Chooβ™šπ•‚π•šπ•Ÿπ•˜
"when I try to console.log" You do this console log inside of UI code?
GamerPro
GamerProOPβ€’13mo ago
Afterwards I for example run this function: function AddParkedCars(ownedCars) { console.log(locales.Servername) } But then it prints: undefined Yes
Chooβ™šπ•‚π•šπ•Ÿπ•˜
What do you get if you console log locales instead of locales.Servername?
GamerPro
GamerProOPβ€’13mo ago
[Object object] πŸ˜…
Chooβ™šπ•‚π•šπ•Ÿπ•˜
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.
GamerPro
GamerProOPβ€’13mo ago
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
Chooβ™šπ•‚π•šπ•Ÿπ•˜
As a test, comment out the entire section where the value of locales is assigned and temporarily replace it with:
locales = {test1: 1, test2: 2}
locales = {test1: 1, test2: 2}
See if the console log works with that.
GamerPro
GamerProOPβ€’13mo ago
Works everywhere.
Chooβ™šπ•‚π•šπ•Ÿπ•˜
The console log with that value is showing properly?
GamerPro
GamerProOPβ€’13mo ago
yes.
Chooβ™šπ•‚π•šπ•Ÿπ•˜
OK. Try assigning only the Servername part and comment out the rest. See if that works.
GamerPro
GamerProOPβ€’13mo ago
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 ❀️
Want results from more Discord servers?
Add your server