There’s something strange with my code and I still can’t figure it out.
I’m trying to randomly sort items from a collection to display them in a repeater.
The whole process triggers onReady of my dataset and does the following:
- Query the Live Collection where I have the data to be sorted.
- Delete all records in the Temporary Collection.
- Add all records from the Live Collection to the Temporary Collection.
- Refresh the Dataset.
- Display the Repeater.
THE CODE
function deleteRecord(collectionName) {
return new Promise((resolve, reject) => {
console.log("**Deleting**");
$w("#preloader").show();
$w("#businessesRepeater").hide();
const numberOfItems = $w('#sortingDataset').getTotalCount();
console.log("Items to Delete: " + numberOfItems);
$w('#sortingDataset').getItems(0, numberOfItems)
.then(results => {
const elements = results.items;
elements.forEach(item => {
wixData.remove(collectionName, item._id);
});
})
.then(() => wixData.query("Members").count())
.then((results) => resolve(console.log("Records in the Collection: " + results)))
.catch((err) => reject(console.log("Error Deleting: " + err)));
});
}
function addRecord(collectionName, sortedItems) {
return new Promise((resolve, reject) => {
console.log("**Adding**");
for (var item in sortedItems) {
wixData.insert(collectionName, sortedItems[item])
.then((results) => {
resolve(console.log("Record Added: " + results._id));
})
.catch((err) => {
reject(console.log("Error Adding: " + err));
});
}
});
}
export function sortingDataset_ready() {
console.log("On Ready Dataset Items: " + $w('#sortingDataset').getTotalCount());
wixData.query("MemberProfile")
.ascending("_id")
.find()
.then((results) => {
items = results.items;
items = shuffle(items);
})
.then(() => deleteRecord("Members"))
.then(() => $w('#sortingDataset').refresh())
.then(() => addRecord("Members", items))
.then(() => $w('#sortingDataset').refresh())
.then(() => {
console.log("Dataset Refreshed - Items: " + $w('#sortingDataset').getTotalCount());
$w("#preloader").hide();
$w("#businessesRepeater").show();
})
.catch((error) => {
console.log("Error Message: " + error.message);
console.log("Error Code: " + error.code);
});
}
THE ISSUE
I get some funny results every time I run the code…
For some reason the delete function is not done when the add function starts to work??
But when the Dataset gets refreshed it shows 9 items…
And the repeater stayed with 6… (o.O)
I would appreciate some help
