Dear Wix Community,
I am currently developing some metrics functions for my website to track views on my product dynamic pages with the following code:
$w.onReady(async function () {
currentProduct = await $w('#dynamicDataset').getCurrentItem()
getMetricsObject().then((results) => {
incrementPageViews(results)
})
}
export function getMetricsObject() {
return wixData.query('ProductMetrics')
.eq('itemId', currentProduct._id)
.find()
.then((results) => {
return results._items[0]
})
}
export function incrementPageViews(currentViewsObject) {
currentViewsObject.profileViews++
wixData.update('ProductMetrics', currentViewsObject)
}
This code works fine in the sandbox environment, with each page load incrementing views by 1. However, when deployed into the production environment, it increments page views by 2 instead.
I tracked the outputs of the functions, and here’s what they look like:
currentView = 0
// In sandbox environment
oldView = 0
newView = 1
// In production environment
oldView = 1
newView = 2
I think this might be related to the asynchronous nature of my functions, but I can’t seem to pinpoint where the bug lies. Do you have any suggestions on how I should fix it? It would be much appreciated!