Creating an Array from a fetch call in a loop works on page code now not backend code.

I am trying to create an array from data i am collecting via Fetch. I have it working so it creates the array in the loop but I cant get it too just output the final array created.

I have tried 2 ways so far, one pushing the promise into an array

wixData.query(“NewFRNData”)
.limit(150)
.find()
.then((results) => {
if (results.items.length > 0) {
let item = results.items;
let totalCount = results.totalCount;
console.log(totalCount);
// Find Number of Items to update
for ( var i = 0; i < totalCount; i++) {
// count number of calls on 10th pause for 10 seconds to manage API
// Get Info From API
var fcanumber = results.items[i]._id;
frnarray.push(getfcainfo(fcanumber))}
return Promise.all(frnarray);
// end of loop for getting FRNs}
}).then((info) => {
console.log(frnarray)});
}
This gives me an Uncaught in promise Error.
The other way was to push the results of the promise into the array.

let frnarray = [];
//Add your code for this event here:
//console.log(“Test Code Start”);
wixData.query(“NewFRNData”)
.limit(150)
.find()
.then((results) => {
if (results.items.length > 0) {
let item = results.items;
let totalCount = results.totalCount;
console.log(totalCount);
var j = 0;
// Find Number of Items to update
for ( var i = 0; i < totalCount; i++) {
// Get Info From API
var fcanumber = results.items[i]._id;
getfcainfo(fcanumber)
.then(fcainfo => {
frnarray.push(fcainfo);
console.log(frnarray)
});
}
// end of loop for getting FRNs
}
})
}
But this way i cant seem to get the final finished array, any help would be appreciated.

Tom,

Your second option looks more promising, so I’m going to make some suggestions about how to make that work.

You are making a lot of calls to fetch data, so the loop continues to execute further calls before it has received the results of the previous ones. Consequently, as constructed, the array never gets populated appropriately.

Have a look at this article to get a better understanding of this phenomena:
https://support.wix.com/en/article/corvid-working-with-promises .

The code uses the async/await approach outlined towards the end of the article with your second option above. The getfcainfo function with the fetch call, presumably in your backend code, will also need to be an async function for this to work.

$w.onReady(function () {
    NewFRNDataQuery();
});
export async function NewFRNDataQuery(){
    const results = await wixData.query("NewFRNData").limit(150).find();
    if (results.items.length > 0) {
        let item = results.items;
        let totalCount = results.totalCount;
        console.log(results);
        var j = 0;
        // Find Number of Items to update
        for (var i = 0; i < totalCount; i++) {
            // Get Info From API
            var fcanumber = results.items[i]._id;
            const call = await Call_getfcainfo(fcanumber);
        }
        // end of loop for getting FRNs
        console.log("frnarray follows");
        console.log(frnarray);
    }
}
export async function Call_getfcainfo(fcanumber){
    const fcainfo = await getfcainfo(fcanumber);
    console.log("fcainfo follows");
    console.log(fcainfo);
    if (fcainfo.length > 0){
        frnarray.push(fcainfo);
    }
}

Thanks for the suggestion, have got it working with some tweeks on the front end. If i move the code to the backend it fails with a “WebMethod request timed-out after 14 seconds… Did you forget to resolve a promise?” Any ideas why?

export async function NewFRNDataQuery() {
console.log(“Started”)
let toupdate = []
var curDate = null ;
const results = await wixData.query(“NewFRNData”).limit(5).find();
if (results.items.length > 0) {
let item = results.items;
console.log(item)
//let totalCount = results.totalCount;
let totalCount = results.length;
console.log(totalCount)
var j = 0;
var frnarray = []
// Find Number of Items to update
for ( var i = 0; i < totalCount; i++) {
// Get Info From API
const fcainfo = await asyncgetfcainfo(results.items[i]._id);
frnarray.push(fcainfo);
}
// end of loop for getting FRNs
}
console.log(frnarray);
console.log(frnarray.length)
}

@tom66859 I haven’t encountered that message, but there’s this posting on Stack Overflow: https://stackoverflow.com/questions/57440441/wix-fetch-call-in-backend-gets-time-out-after-14-seconds-error

@tony-brunsman Thx saw that myself, the Fetch request is coming back though, you get 8 or so in the 14s then it stops. Head hurting now from google searches.