I see a number of issues:
In your page (frontend) code, you need to move your code into the page’s onReady() function. Also, you should also have an onReady() function for the dataset to ensure that it is ready before trying to use it. The wixData.get() doesn’t do anything and isn’t needed. Your page code should look something like this:
$w.onReady(function () {
setInterval(() => $w('#dataset1').refresh(), 500);
});
In your backend code, you are not handling the Promises correctly. I would highly recommend learning more about Promises before continuing. For information on what Promises do and how to properly use them, see the following articles:
As an example, you have the following segment of code:
wixData.get("clickCounter", "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7")
.then((results) => {
newVal = results.value + 1; //see item below
});
let toUpdate = {
"_id": "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7",
"title": "clicks",
"value": newVal
};
The problem with the above code is that toUpdate gets executed before the .get() returns - newVal is not yet set with the retrieved value.
You would need something like this:
wixData.get("clickCounter", "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7")
.then((results) => {
newVal = results.value + 1;
// you can newVal here
let toUpdate = {
"_id": "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7",
"title": "clicks",
"value": newVal
};
});
Another way (which might be easier to read and implement) would be to use await:
let results = await wixData.get("clickCounter", "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7");
newVal = results.value + 1;
let toUpdate = {
"_id": "9ef6dc0d-882a-4368-a5af-1de87c4ed4e7",
"title": "clicks",
"value": newVal
};
You will also need to correctly handle the Promise for the .update().
Good luck.