我正在自動訂閱客戶端上的集合。我的代碼如下。我希望客戶有更新的信息,但它永遠不會更新。除非我刷新頁面,否則此問題將體現在按鈕從不被消息取代。爲什麼我的測試集合沒有被更新?
實際上,我爲'Tests'集合創建的一個文檔應該在客戶端上將其testCount屬性更新爲1,但除非刷新頁面,否則它不會發生。它不應該更新嗎?
未安裝自動發佈軟件包。
在這一點上,我相信流星有一個錯誤,或者我只是不明白一些基本的東西。我即將安裝meteor.js的開發版本並找出它,或者放棄,直到Meteor.js實際上可以工作。 :P
我的控制檯日誌:
Tests updated!
checking test
no test
Tests updated! Object {_id: "ea9f6002-74c7-4f37-9f10-0167b3b6f65a", testCount: 0}
checking test result
test.testCount 0
我的服務器日誌:
$ meteor reset;meteor
Project reset.
[[[[[ ~/Dropbox/projects/sphela-game/sphela ]]]]]
Running on: http://localhost:3000/
inserting new test
Connecting test, getting initial data.
Test added do nothing { testCount: 0, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' }
Connecting test, getting initial data.
Test added do nothing { testCount: 0, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' }
Running test.
Updating test to 1.
Test ran. { testCount: 1, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' }
Test added do nothing { testCount: 1, _id: 'ea9f6002-74c7-4f37-9f10-0167b3b6f65a' }
我的HTML:
<head> <title>Testing counts.</title> </head>
<body> {{> app}} </body>
<template name="app">
{{#if testSuccess}}
<h1>Test complete.</h1>
{{else}}
<button class="btn run-test">Run Test</button>
{{/if}}
</template>
我的javascript:
var Tests = new Meteor.Collection('tests');
if (Meteor.isClient) {
Meteor.startup(function() {
Session.set('testCount', 0)
Meteor.subscribe('connect');
});
Template.app.testSuccess = function() {
var test;
console.log('checking test result');
test = Tests.findOne();
if (!test) {
console.log('no test', test);
return false;
}
console.log('test.testCount', test.testCount);
return test.testCount > 0;
};
Template.app.events({
'click .run-test': runTest
});
function runTest(event) {
Meteor.call('runTest');
Session.set('testCount', 1);
}
Meteor.autorun(function() {
console.log('Tests updated!', Tests.findOne());
});
Meteor.autosubscribe(function() {
Meteor.subscribe('test-results', Session.get('testCount'));
});
}
if (Meteor.isServer) {
Meteor.startup(function() {
test = Tests.findOne({})
if (!test) {
test = {
testCount: 0
};
console.log('inserting new test');
test._id = Tests.insert(test);
} else {
console.log('startup reset');
test.testCount = 0;
Tests.update({_id:test._id}, test);
}
});
Meteor.publish('connect', function() {
var test_;
console.log('Connecting test, getting initial data.');
test_ = Tests.findOne({});
this.set('tests', test_._id, test_);
this.complete();
this.flush();
});
Meteor.publish('test-results', function(test) {
var handle;
handle = Tests.find({testCount: test}).observe({
changed: _.bind(function(test) {
console.log('Test changed', test._id, test.testCount);
this.set('tests', test._id, test);
this.flush();
}, this),
added: _.bind(function(test) {
console.log('Test added do nothing', test);
this.flush();
}, this)
});
this.complete();
this.flush();
this.onStop(function() {
handle.stop();
});
});
Meteor.methods({
runTest: function() {
var test;
console.log('Running test.');
test = Tests.findOne({});
test.testCount = 1;
console.log('Updating test to 1.');
Tests.update({_id: test._id}, test);
console.log('Test ran.', Tests.findOne());
},
});
}
你有安裝包自動發佈? –
完全忘了它。刪除它後,我的體驗與你一樣。 –
那麼這是一個錯誤呢? –