I have a database that contains overall stats for product reviews but when using the wixData.get function it keeps falling into the else when checking the data has been retrieved. Can anyone tell me where I’m going wrong?
async function loadStatistics() {
const stats = await wixData.get('review-stats', product._id);
if (stats) {
console.log("Stats found");
let avgRating = (Math.round(stats.rating * 10 / stats.count) / 10);
console.log("number of rating = " + stats.count);
console.log("average rating = " + avgRating);
let ratings = $w('#generalRatings');
ratings.rating = avgRating;
ratings.numRatings = stats.count;
}
else{
console.log("No stats found");
$w('#generalRatings').numRatings = 0;
$w('#generalRatings').rating = 0;
}
}
Maybe your database is empty. Or it doesn’t have any entries for the product._id, or the collection permissions are incorrect, or all sorts of other things.
You’ll need to provide more information.
Hi Scott 
When using get() , you need to pass the ID of the entry as a parameter, NOT the product ID, if you logged the stats to the console, it’ll print out " null ".
console.log(stats) // Should print null
I assume that there’s a field in your database (perhaps a reference field) to the products collection, If it’s correct, I suggest that you query the stats collection with the product._id in mind.
stats = return wixData.query('review-stats').eq('product', product._id).find().then((result) => {
if (result.length > 0) {
return result.items[0];
} else {
return null;
console.warn('This product has no stats yet!')
}
})
Hope this helps~!
Ahmad
Good catch Ahmad. I told you you were smart.
Thanks for the compliment
It was obvious to me at the first glance.
It doesn’t require genius to spot it, I think you missed it because you was distracted and busy.