I am creating an app that allows users to rate a business from 1 to 5. I am trying to find the average of the ratings to use in my app. So far I am currently only able to see the count with this code.
I am struggling to figure out how to read the all of the rating objects (randomValue: Int). I was thinking it would be best to store them in an array. I am very new to Node so forgive me.
I cannot find anything in the firebase cloud functions docs about this.
The ratings are stored as follows:
My Code:
exports.scoreOneAverage = functions.database.ref('/ratings_average/{business_uid}/Score_1/{random}')
.onWrite(event => {
const collectionRef = event.data.ref.parent;
const countRef = collectionRef.parent.child('count');
// Return the promise from countRef.transaction() so our function
// waits for this async event to complete before it exits.
return countRef.transaction(current => {
if (event.data.exists() && !event.data.previous.exists()) {
return (current || 0) + 1;
}
else if (!event.data.exists() && event.data.previous.exists()) {
return (current || 0) - 1;
}
}).then(() => {
console.log('Counter updated.');
List ratings = event.data.val();
count = countRef.event.data.val();
if( ratings === null ) {
console.log('No ratings');
}
else {
console.log("The average score is:" + count + ratings );
}
});
});
via ethanfox27
No comments:
Post a Comment