peter
1
So, after reading this article: https://support.wix.com/en/article/corvid-working-with-promises , I can’t understand why the following doesn’t work:
$w.onReady(function () {
$w("#dsCourses").onReady(() => {
$w("#dsCourses").setFilter( wixData.filter()
.eq("groupSortOrder", "100")
);
loadDataIntoTxt(0, "groupDescription", "#txtL2DDescription");
loadDataIntoTxt(0, "callout", "#txtSnorkelerCallout");
loadDataIntoTxt(1, "callout", "#txtDiscScubaCallout");
loadDataIntoTxt(2, "callout", "#txtOWDCallout");
});
});
async function loadDataIntoTxt(itemNo, fieldKey, textboxName) {
await $w("#dsCourses").setCurrentItemIndex(itemNo);
let currentItem = await $w("#dsCourses").getCurrentItem();
$w(textboxName).text = currentItem[fieldKey];
}
The loadDataIntoTxt() async function doesn’t seem to await for anything. Am I using await incorrectly?
Thanks for any help!
Some observations:
-
You don’t need await on getCurrentItem()
-
You need await for setFilter()
-
You need await for each call to the async function loadDataIntoTxt()
Your code should look something like this (not tested):
$w.onReady(function () {
$w("#dsCourses").onReady(() => {
await $w("#dsCourses").setFilter( wixData.filter()
.eq("groupSortOrder", "100")
);
await loadDataIntoTxt(0, "groupDescription", "#txtL2DDescription");
await loadDataIntoTxt(0, "callout", "#txtSnorkelerCallout");
await loadDataIntoTxt(1, "callout", "#txtDiscScubaCallout");
await loadDataIntoTxt(2, "callout", "#txtOWDCallout");
});
});
async function loadDataIntoTxt(itemNo, fieldKey, textboxName) {
await $w("#dsCourses").setCurrentItemIndex(itemNo);
let currentItem = $w("#dsCourses").getCurrentItem();
$w(textboxName).text = currentItem[fieldKey];
}
You should again review the article on Promises.
I haven´t tested it either, but shouldn´t the
$w.onReady( function () {
be
$w.onReady(async function () {
once you want to await inside it?
I think actually in this case, the correct scope for the async would be
$w(‘#dsCourses’).onReady(async function() {}