Basically there are two data in two different location and need to be compared at first. The First data is at the Database ChampionCollection(CC DB), which i am locating by performing wixData.query on the CC DB and the second one is at API. Inside the query operation, I'm performing two operation- First, I'm searching the JSON array from the API for value of latestChampion [ if(res[i].latestChampion) === 'y' ] Second, if the "id" of both the data [ one from DB(result.items[0].championId) and the other one from API ( res[i].id ) ] doesn't matches then perform two operations, 1.Update the latestChampion column of CC DB on the specific row that yield value of latestChampion = 'y' to latestChampion='n'
2. Insert the data of the API into CCDB. PROBLEM PART : The wixData.update works fine and does the job, however nothing get executed after the update part and thus wixData.insert operation doesn't perform.
◘ Briefing of getchampion('pc') => In this function, we call the API and get the data(list of champions) from the API in a JSON array.
◘CODE:
$w.onReady(function () {
//Add new champion into champion collection database
$w('#buttonAddNewChamp').onClick((event, $w) => {
wixData.query("ChampionCollection")
.contains("latestChampion", 'y')
.find()
.then((result) => {
//console.log(result.items);
getchampions('pc') //Calls the API getchapions()
.then(res => {
for (var i = 0; i <= res.length; i++) {
if (res[i].latestChampion === 'y') {
if (res[i].id !== result.items[0].championId) {
console.log(res[i].id + "Inside getchampion " + result.items[0].championId);
console.log("BooYAHHH");
$w('#textOperationStatus').text = "New champion Found. Updating...";
let toUpdate = {
"_id": result.items[0]._id,
"title": result.items[0].title,
"championTitle": result.items[0].championTitle,
"championRoles": result.items[0].championRoles,
"championHealth": result.items[0].championHealth,
"championLore": result.items[0].championLore,
"championSpeed": result.items[0].championSpeed,
"image": result.items[0].image,
"championId": result.items[0].championId,
"championPantheon": result.items[0].championPantheon,
"latestChampion": 'y',
"ability1": result.items[0].ability1,
"idAbility1": result.items[0].idAbility1,
"descAbility1": result.items[0].descAbility1,
"imageAbility1": result.items[0].imageAbility1,
"ability2": result.items[0].ability2,
"idAbility2": result.items[0].idAbility2,
"descAbility2": result.items[0].descAbility2,
"imageAbility2": result.items[0].imageAbility2,
"ability3": result.items[0].ability3,
"idAbility3": result.items[0].idAbility3,
"descAbility3": result.items[0].descAbility3,
"imageAbility3": result.items[0].imageAbility3,
"ability4": result.items[0].ability4,
"idAbility4": result.items[0].idAbility4,
"descAbility4": result.items[0].descAbility4,
"imageAbility4": result.items[0].imageAbility4,
"ability5": result.items[0].ability5,
"idAbility5": result.items[0].idAbility5,
"descAbility5": result.items[0].descAbility5,
"imageAbility5": result.items[0].imageAbility5,
"extraImage": result.items[0].extraImage,
"championTeaser": result.items[0].championTeaser
} //END of --toUpdate--
console.log(res[i].Name);
wixData.update("ChampionCollection", toUpdate) //Update the latestChampion column of the old Champ in championCollection DB to 'n'
.then(resUpdate => {
console.log(resUpdate.title + resUpdate.latestChampion + res[i].Name);
$w('#textOperationStatus').text = "Update Complete for : " + resUpdate.title;
})
var toInsert = {
"title": res[i].Name,
"championTitle": res[i].Title,
"championRoles": res[i].Roles,
"championHealth": res[i].Health,
"championLore": res[i].Lore,
"championSpeed": res[i].Speed,
"image": res[i].ChampionIcon_URL,
"championId": res[i].id,
"championPantheon": res[i].Pantheon,
"latestChampion": res[i].latestChampion,
"ability1": res[i].Ability1,
"idAbility1": res[i].AbilityId1,
"descAbility1": res[i].abilityDescription1,
"imageAbility1": res[i].ChampionAbility1_URL,
"ability2": res[i].Ability2,
"idAbility2": res[i].AbilityId2,
"descAbility2": res[i].abilityDescription2,
"imageAbility2": res[i].ChampionAbility2_URL,
"ability3": res[i].Ability3,
"idAbility3": res[i].AbilityId3,
"descAbility3": res[i].abilityDescription3,
"imageAbility3": res[i].ChampionAbility3_URL,
"ability4": res[i].Ability4,
"idAbility4": res[i].AbilityId4,
"descAbility4": res[i].abilityDescription4,
"imageAbility4": res[i].ChampionAbility4_URL,
"ability5": res[i].Ability5,
"idAbility5": res[i].AbilityId5,
"descAbility5": res[i].abilityDescription5,
"imageAbility5": res[i].ChampionAbility5_URL,
"extraImage": "https://web2.hirez.com/paladins/assets/Carousel/" + (res[i].Name).repace(" ", "-") + ".png",
"championTeaser": $w('#inputYoutubeUrl').value
} //End of --toInsert--
wixData.insert("ChampionCollection", toInsert)
.then(resInsert => {
console.log(res);
let item = resInsert;
console.log(item);
$w('#textOperationStatus').text = "Champion successfully inserted: " + resInsert.title;
})
.catch(err => {
let errorMsg = err;
console.log(errorMsg);
$w('#textOperationStatus').text = "Oh boy! Failed to Insert the champion into the DB";
});
} //END of --if(res[i].id !== result.items[0].championId)--
else {
$w('#textOperationStatus').text = "latestChampion is same in both DB and API";
return;
}
} //END of --if(res[i].latestChampion === 'y')--
} //END of --for(var i=0; i<=res.length; i++)--
}); //END of --getchampions(pc).then--
}); //END of --wixData.query(collectionName).then--
}); //END of --buttonAddNewChamp.OnClick-- event
});
Thank you.
PS: Sorry the code is formatted here very poorly, so ive attached a downloadable link here. Thanks
Did a typo, at the 2nd last line under var toInsert = { ...
...
... "extraImage": "https://web2.hirez.com/paladins/assets/Carousel/" + (res[i].Name).repace(" ", "-") + ".png", }
it should be >> (res[i].Name).replace(" ", "-") << and not !>>(res[i].Name).repace(" ", "-") <<! Sorry, my bad. I'm still leaving this post here if anyone need any help on the code or anything.