Hello!
so im using the _id to update a item in my Fighters dataset (will paste the code below). But when i read the Wix velo api documentation i read/understand if i use the _id to find the item i can update the field key without losing my other values. Well this does not work and i dont know how to modify my code so it wont delete all other values in the item
This line of code deletes all value`s from the item
// Updating the fighter to be active
await wixData.update("Fighters", {
"_id": fighterData._id,
"activeFighter": true
});
Code below is the combined code of above and what it needs to do once it got updated.
async function checkAndModifyFighterStatus(member, teamName) {
try {
const results = await wixData.query("Fighters")
.eq("fullName", member)
.limit(1)
.find();
if (results.items.length > 0) {
const fighterData = results.items[0];
console.log(`${member} (_id: ${fighterData._id}) found in Fighters database.`); // Logging _id
if (!fighterData.activeFighter) {
console.log(`%c${member} is not active, activating...`, "color: orange;");
// Updating the fighter to be active
await wixData.update("Fighters", {
"_id": fighterData._id,
"activeFighter": true
});
// Fetching the team data
const teamResults = await wixData.query("TeamRegistrationcustom")
.eq("teamName", teamName)
.limit(1)
.find();
if (teamResults.items.length > 0) {
const teamData = teamResults.items[0];
console.log(`Team ${teamName} (_id: ${teamData._id}) found in TeamRegistrationcustom database.`); // Logging _id
// Checking teams database
if (teamData.tokens && teamData.tokens > 0) {
//Updating the team tokens by deducting 1
await wixData.update("TeamRegistrationcustom", {
"_id": teamData._id,
"tokens": teamData.tokens - 1
});
} else {
console.log("No tokens to deduct or team tokens are not defined.");
}
} else {
console.log("Team not found.");
}
} else {
console.log(`%c${member} is active: YES`, "color: green;");
}
} else {
console.log(`${member} is not found in the Fighters database.`);
}
} catch (error) {
console.log('Error checking and modifying fighter status:', error);
}
}
if some one would be able to adjust the code so it wont break… im kinda confused now… hahah sorry if i dont use proper terms im learning JS coding while im building the website.