I have a user scoring method which I would like to store in a separate leaderboard. Each question is worth 100 points. I would like to check, if a user has given correct answer, check her name in leaderboard data. If its there, add 100 to the score and if its not there add the name and add 100 to the score.
I checked the update method but it requires matching with the id, but as I understand id will be different for different rows in dataset. So I want username to be checked in the database.
Hi Ayush,
IDs are typically better to use because people do occasionally have a name change, but that’s pretty seldom.
Call this function with the full name as the parameter. Adjust collection and field names as needed.
export function UpdateLeaderboard(fullName) {
wixData.query("leaderboard")
.eq("fullName",fullName)
.find()
.then((results) => {
if (results.items.length === 1){
let item = results.items[0];
item.score = item.score + 100;
wixData.update("leaderboard",item)
.then((updateResult) => {
console.log("updated");
})
}
if (results.items.length === 0){
let item = {"fullName": fullName,"score": 100}
wixData.insert("leaderboard",item)
.then((insertResult) => {
console.log("inserted");
})
}
})
}