Being a newbie in the world of JavaScript, AngularJS, and Parse, I am eager to learn. If there are any mistakes in my approach, please do point them out as I aim to gain as much knowledge as possible.
I have been given an assignment that requires me to utilize Parse and AngularJS. The task is to display each company
from my companies
object along with their respective ratings. In Parse, these are stored as separate objects connected by a relation.
Although I have managed to calculate the ratings, I am facing an issue where Angular seems to be overriding the data for the ratings. Instead of displaying each rating, it only shows the last calculated rating. However, when I check the console using console.log(total)
, the numbers are accurate.
Here's the snippet from my controller responsible for fetching companies
:
function getCompanies() {
new Parse.Query(Companies).find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
results[i].relation("ratings").query().find({
success: function(ratings) {
var total = 0;
for (var o = 0; o < ratings.length; o++) {
var stars = parseInt(ratings[o].attributes.stars);
total += stars;
}
total = total / ratings.length;
$scope.$apply(function() {
$scope.companies = results.map(function(obj) {
console.log(total);
return {name: obj.get("name"), rating: total, parseObject: obj};
})
});
}
});
}
}
});
}
In my view
, the code I'm using is:
<div ng-repeat="company in companies">
<h1>{{company.name}}</h1>
<h3>Rating: {{company.rating}}</h3>
</div>
Currently, if I had two companies
with ratings of 2 and 4, Angular would show 4 for both. However, the console will correctly display 2 and 4 separately.
If there are major issues in my code, your feedback is highly valued as I strive to enhance my skills. Thank you for any assistance provided.