Hi Pronil:
Majd’s suggestions on style are good ones, although javascript does allow this and it depends on your style. Getting indentation correct also helps with code assessment.
Now here’s what I have experienced with your save to database function…
function SaveDataToDatabase() {
// Since this is returning a promise if you return the getplayer call you will be able to chain SaveDataToDatabase in a promise sequence where it is called. This might help with exception handling
// Replace -> getplayer($w('#input1').value).then(res => { with ...
return getplayer($w('#input1').value)
.then(res => {
console.log(res);
let toInsert = {
"title": res[0].Name,
"playerId": res[0].Id,
"playerLevel":res[0].Level,
"accountCreated": res[0].Created_Datetime,
"lastLogin": res[0].Last_Login_Datetime,
"winsLoss": res[0].Wins + " " + "/" + " " + res[0].Losses,
"matchAfk": res[0].Leaves,
"masteryLevel": res[0].MasteryLevel,
"region": res[0].Region,
"achievementCompleted": res[0].Total_Achievements,
"rankSeason": "Season: " + res[0].RankedConquest.Season,
"rankName": res[0].RankedConquest.Name,
"rankWinsLoss": res[0].RankedConquest.Wins + " " + "/" + " " + res[0].RankedConquest.Losses,
"rankTp": res[0].RankedConquest.Points,
"rankTier": res[0].RankedConquest.Tier,
"rankAfk":res[0].RankedConquest.Leaves,
"previousRank":res[0].RankedConquest.PrevRank
};
//BELOW IS THE SUPPRESS PERMISSION for INSERTing INTO DATABASE
// **** THESE OPTIONS DO NOT WORK IN FRONT END. THEY ARE BACKEND ONLY
let options = {
"suppressAuth": true,
"suppressHooks": true
};
// ****************************
// wixData.insert() returns a promise. If you return this inside
// a promise then you can chain the .then call backs and make
// your code easier to manage
// Replace this wixData.insert("SUMMARY", toInsert,options) with...
return wixData.insert("SUMMARY", toInsert,options);
})
// This then is now chained to the getPlayer call
.then(results => {
let item = results;
console.log(item) //see item below
})
// This will now catch all promise based errors including ones
// from the getplayer call
.catch(err => {
let errorMsg = err;
console.log(errorMsg);
});
// ***************** WHAT IS THIS SUPPOSED TO DO? ****************
// If you want to redirect to this page after a successful data
// collection insert then you need to move this inside of the .then()
// that contains the results.
// As coded this executes immediately, redirects to a new page and aborts
//your data collection save (so you will never save anything :-)
wixLocation.to("https://pronilchakraborty0.wixsite.com/mysite-1/summary/");
//**********************************************************************
}
So Majd’s suspicions were correct
- your save function is broken. If you make the suggestions I propose and move the wixLocation.to() call to one of the .then() functions, or delete it, you might get your expected results. At a minimum the catch() will show you any errors along the way ![]()
Steve