Hi all
I have what is (hopefully) a very simple question!
I have a button named “button1”
I have collection named “members”,
and a number value field within that collection named “loyalty”
I simply want to code a button (button1) to add +25 to the number value field (loyalty) within the collection (members)
I don’t need to display the number anywhere, keep track of what the number is at or change any text - I simply to make the button add +25 to the field within the collection
this is what I have so far: I simply don’t know the next part to code to add +25 to the field
$w("#dynamicdataset").onReady(()=>{
let currentItem=$w("#dynamicdataset").getCurrentItem();
console.log(currentItem);
console.log(currentItem.loyalty);
Hope you can help!
Thanks in advance!
$w.onReady(()=>{
$w('#dynamicdataset').onReady(()=>{
$w('#button1').onClick(()=>{
let currentItem= $w("#dynamicdataset").getCurrentItem();
console.log(currentItem);
let loyaltyValue = currentItem.loyalty;
console.log("Loyalty-Value: ", loyaltyValue);
let myResultValue = Number(loyaltyValue) + 25;
console.log(myResultValue);
});
});
});
@russian-dima this is the second time you’ve helped me in as many days! thankyou sooo much!!!
when running that code nothing happened so I looked at the logs as it gives me an error:
{…}
My Profile
Line 19
Loyalty-Value: undefined
My Profile
Line 21
NaN
line 19 and 21 are:
console.log(currentItem);
console.log("Loyalty-Value: ", loyaltyValue);
I’m wondering if you happen to know where its going wrong? In the collection, the field ‘loyalty’ is 100% a number value and “My Profile” is the name of the page
Not tested, but try this one…
$w.onReady(()=>{
$w('#dynamicdataset').onReady(()=>{
$w('#button1').onClick(async()=>{console.log("!!!clicked!!!");
let currentItem = await $w("#dynamicdataset").getCurrentItem(); console.log("Current-Item: ", currentItem);
let loyaltyValue = currentItem.loyalty; console.log("Loyalty-Value: ", loyaltyValue);
let myResultValue = Number(loyaltyValue) + 25; console.log("My RESULT: ", myResultValue);
});
});
});
@russian-dima Thankyou so much for the further help!
Unfortunatly I had the same error, but I realised that the undefined NaN error I was getting stands for not a number, it made sense to me that it was giving me this error as prior to the button being clicked it was an empty field (it wasn’t adding +25 to 0, it was trying to add +25 to an empty field) and I managed to fix the error by adding " ? currentItem.loyalty : 0; " therefore changing an empty field to 0.
Currently my code looks like this
$w.onReady(()=>{
$w('#dynamicDataset').onReady(()=>{
$w('#button121').onClick(async()=>{console.log("!!!clicked!!!");
let currentItem = await $w("#dynamicDataset").getCurrentItem();
console.log("Current-Item: ", currentItem);
let loyaltyValue = currentItem.loyalty ? currentItem.loyalty : 0;
console.log("Loyalty-Value: ", loyaltyValue);
let myResultValue = Number(loyaltyValue) + 25;
console.log("My RESULT: ", myResultValue);
});
});
});
It seems to be working perfectly in my console now! YAY!!!
My only small minor issue is now that when I press the button, it gives me a total result of +25 in my console each time I press it (where as I want it to be cumulative and add up)
After going over the code line by line - my logic is telling me that what is happening is that I’m only ever getting the “myResultValue” as a console log, and its never actually changing it in the collection.
Do I need to add a way to save/update/insert myResultValue to become the new “currentItem.loyalty” so that each time the button is clicked the collection updates rather than just logging it in the console, and therefore each time the button is pressed “currentItem.loyalty” should be a different value rather than always being read by currentItem.loyalty as 0?
Or am I completely wrong? haha - im just trying to deduct what might be happening by what makes sense to me even though I dont have a good grasp on whats going on!
Thanks again for your help! you’re an absolute legend!
@russian-dima
I gave it a go - but am I using the save function wrong here? Every time I click the button it creates a new row (a different member/id) within the collection rather than saving it to the Current Item in the collection - It does however save to the correct field - Just with a brand new entry every time its clicked rather then updating the field in the current row of the collection
$w.onReady(()=>{
$w('#dynamicDataset').onReady(()=>{
//add +25 for daily points bonus button
$w('#button121').onClick(async()=> {console.log("!!!clicked!!!");
let currentItem = await $w("#dynamicDataset").getCurrentItem();
console.log("Current-Item: ", currentItem);
let loyaltyValue = currentItem.loyalty ? currentItem.loyalty : 0;
console.log("Loyalty-Value: ", loyaltyValue);
let myResultValue = Number(loyaltyValue) + 25;
console.log("My RESULT: ", myResultValue);
//save to collection
let toSave = {
"loyalty": myResultValue
};
wixData.save("Members", toSave)
.then((results) => {
console.log(results); //see item below
})
.catch((err) => {
console.log(err);
});
});
SOLVED: Final working code below. Thanks so much @russian-dima
$w.onReady(()=>{
$w('#dynamicDataset').onReady(()=>{
//add +25 for daily points bonus button
$w('#button121').onClick(async()=>{console.log("!!!clicked!!!");
let currentItem = await $w("#dynamicDataset").getCurrentItem();
console.log("Current-Item: ", currentItem);
let loyaltyValue = currentItem.loyalty ? currentItem.loyalty : 0;
console.log("Loyalty-Value: ", loyaltyValue);
let myResultValue = Number(loyaltyValue) + 25;
console.log("My RESULT: ", myResultValue);
$w("#dynamicDataset").setFieldValue("loyalty", myResultValue);
$w("#dynamicDataset").save();
});