流星订阅回调运行时订阅包含以前的订阅导致
问题描述:
我是相当新的流星,而我正在与订阅回调一个奇怪的问题。我有一个包含课程和评论的数据库。我在评论上使用发布/订阅模型来返回仅与所选类相关的评论,并且每次单击新类时都希望更改评论。我想打印所有评论并编辑一些关于评论的指标(平均质量,难度评分)。使用下面的代码,以订阅,更新发送到客户端的评论,印刷点评(这是从一个帮手抢下)返回正确,但指标(这是抓住上onReady回调助手)是不准确的。当onReady函数运行时,即使评论本身正确打印,本地评论集合的当前结果也包含点击的类和先前点击的类的联合。流星订阅回调运行时订阅包含以前的订阅导致
我也使用自动跟踪试过,但我得到了同样的结果。在更新之前是否有办法清除以前的订阅结果?
发布:
Meteor.publish('reviews', function validReviews(courseId, visiblity) {
\t \t console.log(courseId);
\t \t console.log(visiblity);
\t \t var ret = null
\t \t //show valid reviews for this course
\t \t if (courseId != undefined && courseId != "" && visiblity == 1) {
\t \t \t console.log("checked reviews for a class");
\t \t \t ret = Reviews.find({class : courseId, visible : 1}, {limit: 700});
\t \t } else if (courseId != undefined && courseId != "" && visiblity == 0) { //invalidated reviews for a class
\t \t \t console.log("unchecked reviews for a class");
\t \t \t ret = Reviews.find({class : courseId, visible : 0},
\t \t \t {limit: 700});
\t \t } else if (visiblity == 0) { //all invalidated reviews
\t \t \t console.log("all unchecked reviews");
\t \t \t ret = Reviews.find({visible : 0}, {limit: 700});
\t \t } else { //no reviews
\t \t \t console.log("no reviews");
\t \t \t //will always be empty because visible is 0 or 1. allows meteor to still send the ready
\t \t \t //flag when a new publication is sent
\t \t \t ret = Reviews.find({visible : 10});
\t \t }
\t \t //console.log(ret.fetch())
\t \t return ret
\t });
订阅:
this.helpers({
reviews() {
return Reviews.find({});
}
});
和订阅调用,在构造与助手:
constructor($scope) {
$scope.viewModel(this);
//when a new class is selected, update the reviews that are returned by the database and update the gauges
this.subscribe('reviews',() => [(this.getReactively('selectedClass'))._id, 1], {
//callback function, should only run once the reveiws collection updates, BUT ISNT
//seems to be combining the previously clicked class's reviews into the collection
onReady: function() {
console.log("class is: ", this.selectedClass);
if (this.isClassSelected == true) { //will later need to check that the side window is open
//create initial variables
var countGrade = 0;
var countDiff = 0;
var countQual = 0;
var count = 0;
//table to translate grades from numerical value
var gradeTranslation = ["C-", "C", "C+", "B-", "B", "B-", "A-", "A", "A+"];
//get all current reviews, which will now have only this class's reviews because of the subscribe.
var allReviews = Reviews.find({});
console.log(allReviews.fetch());
console.log("len is " + allReviews.fetch().length)
if (allReviews.fetch().length != 0) {
console.log("exist")
allReviews.forEach(function(review) {
count++;
countGrade = countGrade + Number(review["grade"]);
countDiff = countDiff + review["difficulty"];
countQual = countQual + review["quality"];
});
this.qual = (countQual/count).toFixed(1);
this.diff = (countDiff/count).toFixed(1);
this.grade = gradeTranslation[Math.floor(countGrade/count) - 1];
} else {
console.log("first else");
this.qual = 0;
this.diff = 0;
this.grade = "-";
}
} else {
console.log("second else");
this.qual = 0;
this.diff = 0;
this.grade = "-";
}
}
})
答
当使用发布 - 订阅客户端上的minimongo数据库将包含订阅的工会,除非他们明确地清除。因此,您希望重复客户端发布中的查询,以便按照相同的方式进行过滤和排序。 Minimongo在客户端的速度非常快,通常你的数据少得多,所以不用担心性能。
在你constructor
您有:
var allReviews = Reviews.find({});
改用:
另一个侧面提示:JavaScript是quite clever约truthy和falsy值。
if (courseId != undefined && courseId != "" && visibility == 1)
可以简化为:
if (courseId && visibility)
假设你使用visibility == 1
表示true
和visibility == 0
表示false
谢谢!这完全解决了问题! –