流星中的這段代碼。流星:模板助手中的例外
Template.message.helpers({
message: function() {
var message = Messages.findOne({ _id: FlowRouter.getParam('messageId')});
var curTime = new Date();
console.log(message.createdAt.getHours());
return message;
}
});
給了我這樣的例外:
Exception in template helper: [email protected]://localhost:3000/app/app.js?hash=0da6be4507e857f169e0cecb7b0874729eae4663:239:13
bindDataContext/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3051:14
Blaze._wrapCatchingExceptions/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1715:14
wrapHelper/</<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3103:14
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744:12
wrapHelper/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3102:12
[email protected]://localhost:3000/packages/spacebars.js?hash=ebf9381e7fc625d41acb0df14995b7614360858a:234:13
template.message.js/Template.message</<@http://localhost:3000/app/app.js?hash=0da6be4507e857f169e0cecb7b0874729eae4663:85:31
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2086:20
viewAutorun/</<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1934:18
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744:12
viewAutorun/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1932:14
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2271:12
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1931:12
[email protected]://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:339:5
[email protected]://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:229:5
[email protected]://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:613:11
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1944:14
Blaze._materializeView/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2080:5
[email protected]://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:640:12
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2079:3
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1532:9
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1474:3
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1483:7
Blaze._materializeView/<@http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2113:25
[email protected]://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:640:12
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2079:3
[email protected]://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2370:3
[email protected]://localhost:3000/packages/kadira_blaze-layout.js?hash=dbd1396d04e62378fc8792cdef18869a1108cedd:204:5
render/</<@http://localhost:3000/packages/kadira_blaze-layout.js?hash=dbd1396d04e62378fc8792cdef18869a1108cedd:77:9
[email protected]://localhost:3000/packages/meteor.js?hash=27829e936d09beae3149ecfbf3332c42ccb1596f:1077:17
withoutInvocation/<@http://localhost:3000/packages/meteor.js?hash=27829e936d09beae3149ecfbf3332c42ccb1596f:464:26
Meteor.bindEnvironment/<@http://localhost:3000/packages/meteor.js?hash=27829e936d09beae3149ecfbf3332c42ccb1596f:1105:17
[email protected]://localhost:3000/packages/meteor.js?hash=27829e936d09beae3149ecfbf3332c42ccb1596f:401:11
meteor.js:930:11
它實際上會打印兩次,但我覺得這是很大的,沒有必要在這裏兩次展示。
app.js的第239行看起來像這樣(這裏是第7行)。
Template.message.helpers({ // 34
message: function() { // 35
var message = Messages.findOne({ // 36
_id: FlowRouter.getParam('messageId') // 36
}); // 36
var curTime = new Date(); // 37
console.log(message.createdAt.getHours()); // 38
return message; // 40
} // 41
但時間仍然打印到控制檯。
我的目標是使用createdAt日期從當前日期對象中減去它並獲得時間增量。但是我從來沒有那麼遠,因爲無論我嘗試如何處理message.createdAt都會產生這種奇怪的異常。
如果我只是做console.log(message.createdAt),我會在瀏覽器控制檯中看到兩個ISO格式的日期,以及兩個例外。
如果我嘗試其他類似message.createdAt.getTime(),我得到一個Unix時間戳。
這是怎麼回事?
我實際上能夠減去日期並按預期使用它,但是儘管代碼正常工作,但例外情況仍然顯示出來。
您看到重複項,因爲助手正在多次運行(由於被動更改)。 JS的日期對象'createdAt'是什麼? – chazsolo
它來自mongo集合,顯示爲mongo shell中的ISODate,但我認爲它是JS中的Date對象。我可以使用像Date和getTime()這樣的日期屬性。 –
您是否正在等待訂閱在呈現/調用該幫助器之前完成? – chazsolo