I am having issues fully grasping the Promise and Async aspects of the backend in wix. I have the following code that isn’t in the proper sync:
Code on the frontend:
confirmPokemonId($w('#input1'))
.then( (results) => {
wixLocation.to("/searched-pokemon-1/" + results.items[0].title);
});
Code on the backend:
export async function confirmPokemonId(input) {
let temp = await wixData.query("SearchedPokemon").eq("pokemonId", input.value).find();
if(temp.items.length > 0) {
return temp;
} else {
let obj = await apiCall("pokemon", input.value);
return await addPokemon(obj);
}
}
export function addPokemon(obj) {
...code removed for brevity...
return wixData.query("SearchedPokemon")
.eq("title", item.title)
.find(options)
.then( (results) => {
if(results.items.length > 0) {
return results;
} else {
return wixData.insert("SearchedPokemon", item, options)
.then( temp => temp);
}
});
}
What am I doing wrong? I have two theories. Either I don’t understand what WixData.insert returns. Or WixLocation is executing before the Pokemon is added to the Database.