Hallo Wix-Community,
i have some trouble with my code, which has a strange behaviour.
I have 2x Databases/Collections. The first one, called [Schwarzmarkt] has entries (items) made by different user and the second one, called [SM-Verwaltung], which has to collect just user-defined items (has to collect all the data of the user, which is currently logged in).
The code shown below, works, but not properly (not 100%)…
import wixData from 'wix-data';
import wixUsers from 'wix-users';
var BENUTZER
var BenutzerId
var Gefundeneitems
var selectedItem
$w.onReady(function () {
BENUTZER = wixUsers.currentUser;
BenutzerId = BENUTZER.id;
console.log("BENUTZER = " + BENUTZER)
console.log("Benutzer-ID = " + BenutzerId)
$w("#input3").value = BenutzerId
füllung_DBsmverwaltung();
});
function füllung_DBsmverwaltung (parameter) {
wixData.truncate("SM-Verwaltung")
let DatenQuery = wixData.query("Schwarzmarkt")
.eq("_owner", BenutzerId)
.find()
.then((Results) => {
let Gefundeneitems = Results.items;
let Datenlänge = Results.length;
console.log(Datenlänge)
for (var i = 0; i < Gefundeneitems.length; i++) {
console.log(i)
let toInsert = {
"title": Gefundeneitems[i].title,
/*
.....here are some more definitions (columns)
*/
}
wixData.insert("SM-Verwaltung", toInsert).then( (results) => {let item = results;})
}
})
}
And here a summary of what happens when the code runs…
The problem is, that if there are more than 11 or 12 items in [Schwarzmarkt], after the code has done his job, not all items of a specific user have been found.
In this example 12-Items have been found of 17. The result had to be —> 17 !
Is there anybody, who could explain all that behaviour step by step.
I think it is like always a problem with ----> .then() / await / async-coding (asynchronous code)
which is really not of my strengths.
Thank for your HELP an EXPLANATION!
EDIT:
A promise-error like it seems to be, but whats the problem and how to solve?