I have a collection called NewFRNData which is queries to get a list of endpoints. I then create a loop to go through the array and make a fetch request and then update the info in the collection NewFRN. It all works but it is giving an Cannot read property ‘_id’ of undefined on the 3rd from bottom console.log. I think its due to my lack of using the promise and return stuff correctly and help would be greatly appreciated as its so close to working perfectly.
export function getinfo() {
let fcanumber = “”
wixData.query(“NewFRNData”)
.limit(100)
.isNotEmpty(“_id”)
.find()
.then((results) => {
if (results.items.length > 0) {
let item = results.items;
let totalCount = results.totalCount;
var j = 0
// Find Number of Items to update
for ( let i = 0; 1 < totalCount; i++) {
// count number of calls on 10th pause for 10 seconds to manage API
j = j + 1
if (j > 10) {
pausecode(‘10000’);
console.log(“Hello pause” + i);
j = 0
}
// Get Info From API
getfcainfo(item[i]._id)
.then(fcainfo => {
const collection = (‘NewFRNData’)
let toinsert = {
“_id”: fcainfo.Data[0].frn,
“frn”: fcainfo.Data[0].frn,
“organisationName”: fcainfo.Data[0][“Organisation Name”],
“status”: fcainfo.Data[0].status,
“statusEffectiveDate”: fcainfo.Data[0][“Status Effective Date”],
“systemTimestamp”: fcainfo.Data[0][“System Timestamp”],
}
wixData.update(collection, toinsert)
})
// end of loop for getting FRNs
}}
})
. catch ((error) => {
let errorMsg = error.message;
let code = error.code;
console.log("here: " + errorMsg)
});
}