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
ChooKing
ChooKing8mo ago
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
GamerPro8mo ago
The UI is only opened via the event listener.
ChooKing
ChooKing8mo ago
"when I try to console.log" You do this console log inside of UI code?
GamerPro
GamerPro8mo ago
Afterwards I for example run this function: function AddParkedCars(ownedCars) { console.log(locales.Servername) } But then it prints: undefined Yes
ChooKing
ChooKing8mo ago
What do you get if you console log locales instead of locales.Servername?
GamerPro
GamerPro8mo ago
[Object object] 😅
ChooKing
ChooKing8mo ago
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
GamerPro8mo 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
ChooKing
ChooKing8mo ago
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
GamerPro8mo ago
Works everywhere.
ChooKing
ChooKing8mo ago
The console log with that value is showing properly?
GamerPro
GamerPro8mo ago
yes.
ChooKing
ChooKing8mo ago
OK. Try assigning only the Servername part and comment out the rest. See if that works.
GamerPro
GamerPro8mo 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 ❤️