Enter table items into database collection

Hello, I’m new to Wix Code.
I want to put each row of a table into a database. Here is my code but the console say “tableRows[row] is not defined”. But how can I do a loop to insert each row of my #table1 table into my database?

export function insertOutils(owner, $w){
let tableRows = $w(‘#table1’).rows;
tableRows.forEach( (row) => {
let outil = tableRows[row][“nomOutil”];
let categorie = tableRows[row][“categorieOutil”];
let ensembleOutils = {
“nomOutil”: outil,
“categorieOutil”:categorie
}
wixData.insert(“OutillagePossession”, ensembleOutils)
.then( (results) => {
let outils = results;
console.log("outils dans la DB: " + JSON.stringify(outils));
})
. catch ( (err) => {
let errorMsg = err;
});
});
}

Thank you.

So it works with this code :

export function insertOutils(owner, $w){
let tableRows = $w(‘#table1’).rows;
let tableLength = tableRows.length;
console.log(tableLength);
for ( let i = 0; i < tableLength; i++) {
let outil = tableRows[i][“nomOutil”];
let categorie = tableRows[i][“categorieOutil”];
console.log(outil);
console.log(categorie);
let ensembleOutils = {
“nomOutil”: outil,
“categorieOutil”:categorie
}
wixData.insert(“OutillagePossession”, ensembleOutils)
.then( (results) => {
let outils = results;
console.log("outils dans la DB: " + JSON.stringify(outils));
})
. catch ( (err) => {
let errorMsg = err;
});
};
}

But I have another problem :
The field “nomOutil” of my first database (used in this code) is reference to another collection collumn. Each time that I use the function, it said “Reference is broken” so I can’t show my items in a table. I already check, my collumn in my second database where I refered is “nomOutil” too.
How can I retrieve the value of this field compared to the same field in my second database?
Thank you for your help!

Ok it seems to work with this code :

//function to create an array to insert in a DB
function creationEnsemble(outil, categorie) {
let ensembleOutils = {
“nomOutil”: outil,
“categorieOutil”: categorie
}
return ensembleOutils;
}

export function insertOutils(owner, $w){
let tableRows = $w(‘#table1’).rows;
let tableLength = tableRows.length;

for ( let i = 0; i < tableLength; i++) {
let outil = tableRows[i][“nomOutil”];
let categorie = tableRows[i][“categorieOutil”];

//we retrieve item ID in the referenced collection to put it in the right field in the reference collection
wixData.query(‘Outillage’)
.eq(‘nomOutil’,outil)
.find()
.then((results) => {
let outilRef = {ref: ${results.items[0]._id}};

//call to a function to create an array to insert later
let ensembleOutils = creationEnsemble(outilRef, categorie);

// we insert the array in the right database
wixData.insert(“OutillagePossession”, ensembleOutils)
.then( (results) => {
let outils = results;
})
. catch ( (err) => {
let errorMsg = err;
});
})
};
}
}