How to store clicks count in database?

The previous code worked for me, but I didn’t realize that the URL wasn’t working, that is, when I clicked it didn’t take me anywhere, but I already fixed it a bit and when I clicked now it does lead to the URL.

// GET THE CURRENT VALUE OF THE COUNTER FROM THE DATABASE WHEN LOADING THE PAGE 🚀
$w.onReady(() => {
    $w("#dynamicDataset").onReady(() => {
        const itemObj = $w("#dynamicDataset").getCurrentItem();
        const clickCount = itemObj.clicks || 0;
        $w("#clickCounter").text = clickCount.toLocaleString();
    });
});

$w("#button1").onClick(() => {
    $w("#dynamicDataset").onReady(() => {
        const itemObj = $w("#dynamicDataset").getCurrentItem();
        const clickCount = itemObj.clicks || 0;
        const newClickCount = clickCount + 1;
        itemObj.clicks = newClickCount;
        $w("#dynamicDataset").setFieldValue("clicks", newClickCount);

        // Get the URL from the dataset item
        const url = itemObj.newField;
        if (url) {
            // Navigate to the URL
            wixLocation.to(url);
        }

        $w("#dynamicDataset").save()
            .then(() => {
                $w("#clickCounter").text = newClickCount.toLocaleString();
            });
    });
});

1 Like