as far as I know I cannot get an array of structs from solidity to javascript, so make individual request for each request from javacript:
const address = context.params.id;
const campaign = Campaign(address);
const requestCount = await campaign.methods.getRequestsCount().call();
const donatorsCount = await campaign.methods.donatorsCount().call();
// this returns: requests.count 4
console.log("requests.count", requestCount);
const requests = await Promise.all(
Array(requestCount)
.fill() // I tried fill(0)
.map((element, index) => {
return campaign.methods.requests(index).call();
})
);
console.log("requests", requests);
I have 4 requests but I always get only the first request
The requestCount contains string value 4.
And because it's not a Number type specifying the array length, the Array() constructor takes it as one element (value "4") of the newly created array.
Solution: pass the value as type Number to create a 4-item array.
Array(parseInt(requestCount))
Instead of looping through each index, you can also create a Solidity function that returns the whole array
function getRequests() external view returns (Request[] memory) {
return requests;
}
and call it from JS
const requests = await campaign.methods.getRequests().call();