Code works but throws an error at the end which is caught but cant see why. Cannot read property '_id' of undefined

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)
});
}

What are you trying to do with your code insert or update?

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)

You write code for insert and yet you use Wix Data Update.

You can’t do insert in an update.

Insert an item into a collection

import wixData from 'wix-data';

// ...

let toInsert = {
  "title":        "Mr.",
  "first_name":   "John",
  "last_name":    "Doe"
};

wixData.insert("myCollection", toInsert)
  .then( (results) => {
		let item = results; //see item below
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );

/*  item is:
 *
 *  {
 *    "_id":          "rifk4nrk-dj4o-djhe-oidk-fnoqw4oiglk4i",
 *    "_owner":       "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
 *    "_createdDate": "2017-05-24T12:33:18.938Z",
 *    "_updatedDate": "2017-05-24T12:33:18.938Z",
 *    "title":        "Mr.",
 *    "first_name":   "John",
 *    "last_name":    "Doe"
 *  }
 */

Update an item with a specified ID in a collection

import wixData from 'wix-data';

// ...

let toUpdate = {
  "_id":          "00001",
  "title":        "Mr.",
  "first_name":   "John",
  "last_name":    "Doe"
};

wixData.update("myCollection", toUpdate)
  .then( (results) => {
		let item = results; //see item below
	} )
	.catch( (err) => {
		let errorMsg = err;
	} );

/*  item is:
 *
 *  {
 *    "_id":          "00001",
 *    "_owner":       "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
 *    "_createdDate": "2017-05-24T12:33:18.938Z",
 *    "_updatedDate": "2017-05-24T12:33:18.938Z",
 *    "title":        "Mr.",
 *    "first_name":   "John",
 *    "last_name":    "Doe"
 *  }
 */

Finally, for your error message, have you got an element like a text box setup to be shown if there is an error? If so, then simply try changing your code to specify that element

Thanks for the suggestions, i have update the variable name of what i want to update to to update and also pout in some erroe catching at the WixData.update().

let toupdate = {
“_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”],
}
console.log(fcainfo.Data[0][“FRN”]);
wixData.update(collection, toupdate)
.then((result) => {
let itemsadded = results;
console.log(itemsadded); //see item below
})
. catch ((err) => {
let errorMsg = err;
});

The error message is showing in the console and no onpage elements at all. I think its to do with the structure and a missing return but TBH im guessing from Google searches.

for ( let i = 0; 1 < totalCount; i++) {

There’s the source of your error. That should be i, not 1.