我是相當新的流星,而我正在與訂閱回調一個奇怪的問題。我有一個包含課程和評論的數據庫。我在評論上使用發佈/訂閱模型來返回僅與所選類相關的評論,並且每次單擊新類時都希望更改評論。我想打印所有評論並編輯一些關於評論的指標(平均質量,難度評分)。使用下面的代碼,以訂閱,更新發送到客戶端的評論,印刷點評(這是從一個幫手搶下)返回正確,但指標(這是抓住上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 = "-";
}
}
})
謝謝!這完全解決了問題! –