I'm passing an object to a svelte component (from the Electron main process) and while it shows up perfectly in the console, I cannot access the array inside it. However, when I copy the object from the console (or using JSON.stringify(testObject)
) and use it directly (-> copiedObject
), it works:
<script>
let testObject = {};
window.electronAPI.testDataM2R((m2rData) => {
testObject = m2rData;
console.log("testObject:", testObject);
});
let copiedObject = {
filename: "testResults.csv",
readRows: 3,
invalidRows: 0,
finishedReading: true,
data: [
{
tcName: "",
tcSuccess: true,
tcFunctions: [],
},
{
tcName: "TestClass",
tcSuccess: false,
tcFunctions: [
{
tfName: "testBasics",
tfSuccess: true,
tfEntries: [
{
teSuccess: true,
teLine: "6",
teAssertType: "ASSERT_EQUAL",
teComment: "1 == 1",
},
],
},
{
tfName: "basicTest",
tfSuccess: false,
tfEntries: [
{
teSuccess: false,
teLine: "145",
teAssertType: "ASSERT_EQUAL",
teComment: "0.25 == p.getTest()",
},
],
},
],
},
],
};
</script>
<p>{JSON.stringify(testObject)}</p> <!-- this output is exactly the same as the line below -->
<p>{JSON.stringify(copiedObject)}</p> <!-- this output is exactly the same as the line above -->
<p>{testObject.filename}</p> <!-- this works -->
<p>{copiedObject.filename}</p> <!-- this works -->
<p>{testObject.data[1].tcFunctions[0].tfName}</p> <!-- this DOES NOT work -->
<p>{copiedObject.data[1].tcFunctions[0].tfName}</p> <!-- this works -->
So the last two lines are the important bit - why does testObject.data[1].tcFunctions[0].tfName
give me:
Uncaught TypeError: Cannot read properties of undefined (reading '1')
While this copiedObject.data[1].tcFunctions[0].tfName
does, which is just the output from {JSON.stringify(testObject)}
?
Obviously, I need to be able to iterate through the object arrays in order to display all the elements, so I would really appreciate some help on this!
Thank you in advance!