I want to show the current user his information which are stored in my DB. It is working fine. The only problem is to wait for the data to be loaded. In my code below I am using the await keyword to wait for my data to load. But however the onReady function will be triggered twice and also my json object does not include the keyword, but it definitely does.
import wixData from "wix-data";
import wixUsers from "wix-users";
async function getTeamData() {
let user = wixUsers.currentUser;
let user_id = user.id;
let options = {
suppressAuth: true,
suppressHooks: true,
};
wixData
.get("Members/PrivateMembersData", user_id, options)
.then((results) => {
let item = results;
let email = item.loginEmail;
let data;
console.log("Compare email: ", email);
wixData
.query("Users")
.eq("Owner", email)
.find()
.then((found) => {
if (found.items.length > 0) {
data = found.items;
console.log(data);
return data;
} else {
console.log("No matching data found!");
return null;
}
});
})
.catch((error) => {
let error_msg = error.message;
let code = error.code;
console.log(code, error_msg);
});
console.log("Function finished!");
}
function waitForLoading() {
setTimeout(() => {
$w("#preloader").hide("FadeOut");
}, 1500);
}
$w.onReady(async function () {
let data = null;
do {
data = await getTeamData();
} while (data != null);
console.log(data);
let group_name = data[0]["groupName"];
console.log(group_name);
$w("#groupNameTxT").text = group_name;
waitForLoading();
});
Had have anyone a similar problem and know how to solve this? Thanks in advance!