Question:
My code works perfectly when it comes to visual display at the page but the database adds a “1” after the earlier number of likes so if it is 1 from the start > next click makes the database show 11 (should be 2) and next click after that make it show 111 (should be 3).
Product:
Wix Editor
$w(“#emptyHeart1”).onClick(() => {
wixData.get(“Members”, memberId)
.then((item) => {
$w(“#emptyHeart1”).collapse();
$w(“#fullHeart1”).expand();
$w(“#likes1”).text = (Number($w(“#likes1”).text) + 1).toLocaleString();
item.likes1 = true;
wixData.update(“Members”, item).catch();})
})
})
Try converting it to parseInt before adding 1?
Thanks, would you like to show me how you would written it in my code?
Something like this:
$w('#emptyHeart1').onClick(() => {
wixData.get('Members', memberId)
.then((item) => {
$w('#emptyHeart1').collapse();
$w('#fullHeart1').expand();
$w('#likes1').text = (parseInt($w('#likes1').text) + 1).toString();
item.likes1 = true;
wixData.update('Members', item).catch();
});
});
Thank you very much but when I had no likes at all and clicked the like-button it stood “NA” on my page (“not aviable” i suppose?). Then when I gave another click it stood 3 in my database instead of 2. What can the reason be for this?
Now I changed the file type from text to number and then it worked in the database but not on the page, but I can probably solve that in some way.
This might be due to the fact that you are using a dynamic event handler for your button. I have also had some problems with dynamic event handlers when it involves wixData operations, which is why I’ll advise that you use a static onClick handler (export function) for the button instead.
And regarding NA showing up, make sure you set the text to 0 in the editor by default, that should solve it.
This works 
For inserting likes to the database and display it on the page
$w(“#emptyHeart1”).onClick((event) => {
wixData.get(“Members”, memberId)
.then((memberInfo) => {
$w(‘#emptyHeart1’).collapse();
$w(‘#fullHeart1’).expand();
$w(‘#likes1’).text = (parseInt($w(‘#likes1’).text) + 1).toString();
memberInfo.likes1 = true;
wixData.update(‘Members’, memberInfo).catch();
})
});
For getting inserted data at page load
let url = wixLocation.url;
let memberId = url.substr(url.lastIndexOf("/") + 1);
$w.onReady(async function () {
let memberInfo = await getMemberInfo(memberId);
$w(“#likes1”).text = memberInfo.likes1.toString();
});
async function getMemberInfo(memberId) {
let query = wixData.query(“Members”)
.eq(“_id”, memberId)
.find();
let results = await query;
if (results.items.length > 0) {
return results.items[0];
} else {
console.error(Member with ID ${memberId} not found
);
return {
likes: “0”,
};
}
}
When I used this kod for removing a like it did only work at the page but the database added another like instead of removing one, so can you see the fault in this code?
$w(“#fullHeart1”).onClick((event) => {
wixData.get(“Members”, memberId)
.then((memberInfo) => {
$w(‘#fullHeart1’).collapse();
$w(‘#emptyHeart1’).expand();
$w(‘#likes1’).text = (parseInt($w(‘#likes1’).text) - 1).toString();
memberInfo.likes1 = true;
wixData.update(‘Members’, memberInfo).catch();
})
});