Code ninjas can you spot the error in the following code? (Graph chart filtering using buttons and table.)

Hi all,
I have a graph chart on members custom made area displaying unique information to each member by reading a table connected to a database collection. Graph charts display on the click of 2 buttons (2 different charts). Both charts are set to hidden on load, and .show only when the member clicks the button.

However this works on many occasions in others it does not work (statistically it works 1/3 times the page loads).

The issue: On click of any of the buttons the chart fails to load (the graph disappears).
Regardless of result (whether it loads or not) I also get this error in chrome developer tools:
console.js:35 Wix code SDK error: The message parameter that is passed to the postMessage method cannot be set to the value . It must be of type object,string,number,boolean,array.
Any ideas? Thanks.
See the code below:

//Graph code
import wixData from 'wix-data';
import {monthSort} from 'public/months';
$w.onReady(function () {
$w("#table2").columns = [
{
"id": "col1",       // ID of the column for code purposes
// The field key in the collection whose data this column displays
"dataPath": "month",
"label": "Month", // The column header
"width": 100,       // Column width
"type": "string",   // Data type for the column

// Path for the column if it contains a link

},
{
"id": "col2",
"dataPath": "numberoftrades",
"label": "Number of Trades",
"visible": true,
"type": "string",

}
];
wixData.query("Statistics")

.eq("fxraccountnumber", "repay")

.ascending("month")
.limit(1000)  // Max limit of 1,000 //
.find()
.then( (result) => {
const months = result.items.map(x => x.month)
.filter((obj, index, self) => index === self.indexOf(obj))
.sort(monthSort);
const aggregated = months.map(x => {
return {
month: x,

numberoftrades: result.items.filter(obj => obj.month === x)
.map(z => z.numberoftrades)
.reduce(( current) =>  current)
};
});
$w("#table2").rows = aggregated;
let tableArr = [];
for ( var i = 0; i < $w("#table2").rows.length; i++ ) {
tableArr.push (
$w("#table2").rows[i]["numberoftrades"]
)
}
$w("#html1").postMessage(tableArr);
});
});

export function showgraph_click(event) {
$w("#html1").show();
$w("#html3").hide();
$w("#html5").hide();
$w("#clickedmessage2").hide();
let numberoftrades = $w("#input1").value;

$w("#table2").columns = [
{
"id": "col1",       // ID of the column for code purposes
// The field key in the collection whose data this column displays
"dataPath": "month",
"label": "Month", // The column header
"width": 100,       // Column width
"type": "string",   // Data type for the column

// Path for the column if it contains a link

},
{
"id": "col2",
"dataPath": "numberoftrades",
"label": "Number of Trades",
"visible": true,
"type": "string",

}

];

wixData.query("Statistics")

.eq("fxraccountnumber", numberoftrades)

.ascending("month")
.limit(1000)
.find()
.then( (result) => {
const months = result.items.map(x => x.month)
.filter((obj, index, self) => index === self.indexOf(obj))
.sort(monthSort);
const aggregated = months.map(x => {
return {
month: x,

numberoftrades: result.items.filter(obj => obj.month === x)
.map(z => z.numberoftrades)
.reduce((current) => current)
};
});

$w("#table2").rows = aggregated;
let tableArr = [];
for ( var i = 0; i < $w("#table2").rows.length; i++ ) {
tableArr.push (
$w("#table2").rows[i]["numberoftrades"]
)
}
$w("#html1").postMessage(numberoftrades[months]);
$w("#html1").onMessage((event)=>{
if(event.data.type === 'ready'){
$w("#html1").postMessage(numberoftrades[months]);
}

if(event.data.type === 'click'){
$w("#clickedMessage").text = `The total number of trades in ${event.data.label} is ${event.data.value} .`;
$w("#clickedMessage").show();
}
});
console.log(tableArr);
$w("#html1").postMessage(tableArr);
});

}
//repay graph
$w.onReady(function () {
$w("#table3").columns = [
{
"id": "col1",       // ID of the column for code purposes
// The field key in the collection whose data this column displays
"dataPath": "month",
"label": "Month", // The column header
"width": 100,       // Column width
"type": "string",   // Data type for the column

// Path for the column if it contains a link

},
{
"id": "col2",
"dataPath": "repay",
"label": "Total Repay",
"visible": true,
"type": "string",

}

];
wixData.query("Statistics")

.eq("fxraccountnumber", "repay")

.ascending("month")
.limit(1000)  // Max limit of 1,000 //
.find()
.then( (result) => {
const months = result.items.map(x => x.month)
.filter((obj, index, self) => index === self.indexOf(obj))
.sort(monthSort);
const aggregated = months.map(x => {
return {
month: x,

repay: result.items.filter(obj => obj.month === x)
.map(z => z.repay)
.reduce(( current) =>  current)
};
});
$w("#table3").rows = aggregated;
let tableArr = [];
for ( var i = 0; i < $w("#table3").rows.length; i++ ) {
tableArr.push (
$w("#table3").rows[i]["repay"]
)
}
$w("#html5").postMessage(tableArr);
});
});

export function repaygraph_click_1(event) {
$w("#html5").show();
$w("#html1").hide();
$w("#html3").hide();
$w("#clickedMessage").hide();
let repay = $w("#input1").value;

$w("#table3").columns = [
{
"id": "col1",       // ID of the column for code purposes
// The field key in the collection whose data this column displays
"dataPath": "month",
"label": "Month", // The column header
"width": 100,       // Column width
"type": "string",   // Data type for the column

// Path for the column if it contains a link

},
{
"id": "col2",
"dataPath": "repay",
"label": "Total Repay",
"visible": true,
"type": "string",

}

];

wixData.query("Statistics")

.eq("fxraccountnumber", repay)

.ascending("month")
.limit(1000)
.find()
.then( (result) => {
const months = result.items.map(x => x.month)
.filter((obj, index, self) => index === self.indexOf(obj))
.sort(monthSort);
const aggregated = months.map(x => {
return {
month: x,

repay: result.items.filter(obj => obj.month === x)
.map(z => z.repay)
.reduce((current) => current)
};
});

$w("#table3").rows = aggregated;
let tableArr = [];
for ( var i = 0; i < $w("#table3").rows.length; i++ ) {
tableArr.push (
$w("#table3").rows[i]["repay"]
)
}
$w("#html5").postMessage(repay[months]);
$w("#html5").onMessage((event)=>{
if(event.data.type === 'ready'){
$w("#html5").postMessage(repay[months]);
}

if(event.data.type === 'click'){
$w("#clickedmessage2").text = `The total number of repay in ${event.data.label} is $${event.data.value}.`;
$w("#clickedmessage2").show();
}
});
console.log(tableArr);
$w("#html5").postMessage(tableArr);
});

}

The error itself is telling you what the issue is here.
The message parameter that is passed to the postMessage method cannot be set to the value. It must be of type object,string,number,boolean,array.

Basically you need to change your code so that you are not using the value and using as it says, with either an object, string, number, boolean or an array.

You use code to send and receive messages between your page and your HTML element.
You can send and receive data as any valid JavaScript type .

See here for more info.
https://support.wix.com/en/article/corvid-working-with-the-html-element#embedding-another-site
https://www.wix.com/corvid/reference/$w.HtmlComponent.html#onMessage
https://www.wix.com/corvid/reference/$w.HtmlComponent.html#postMessage

Plus, have a read of this page too.
https://www.dyn-web.com/tutorials/iframes/postmessage/
The postMessage method supports data types including strings, numbers, arrays and objects. However, some older browsers only support passing strings in postMessage.

If you look at the chart example from Wix here which it looks like you have used for this.
https://www.wix.com/corvid/example/create-a-custom-chart

Then you will notice that the data used for that tutorial is stored in an array at the top of it, whereas your code is the values coming from your dataset query.

Wix Example.

let year = 2017;
let flights = {
2014: [6.3, 2.4, 7.6, 5.4, 9.9, 7.8],
2015: [6.7, 2.2, 11.2, 5.5, 10.1, 7.9],
2016: [7.2, 3.1, 8.2, 5.6, 9.2, 10.2],
2017: [7.4, 3.9, 8.8, 6.1, 8.7, 9.8]
};
// Data for years is kept in arrays for each year.