.pluck returning undefined in Meteor -
trying pull list of ratings collection of reviews , average them come aggregated average rating plate. when @ data output ratings variable nothing "undefined undefined undefined".
averagerating: function() { var reviews = reviews.findone({plateid: this._id}); var ratings = _.pluck(reviews, 'rating'); var sum = ratings.reduce(function(pv, cv){return pv + cv;}, 0); var avg = sum / ratings.length; //testing output var test = ""; var x; (x in reviews) { text += reviews[x] + ','; } return test; }
sorry if super newbie question, i've been @ hours , cannot figure out.
i figured out issue. listed above var reviews
gets set cursor apparently .pluck not work on. first converting cursor array of objects able use .pluck. updated code looks this:
averagerating: function() { var reviewscursor = reviews.find({plateid: this._id}); //converts cursor array of objects var reviews = reviewscursor.fetch(); var ratings = _.pluck(reviews, 'rating'); var sum = ratings.reduce(function(pv, cv){return pv + cv;}, 0); var avg = (sum / ratings.length).toprecision(2); return avg; }
Comments
Post a Comment