Javascript 在Meteor中处理数据库中的数据
在我的应用程序中,我有一个名为Messages的集合。定义如下:Javascript 在Meteor中处理数据库中的数据,javascript,meteor,Javascript,Meteor,在我的应用程序中,我有一个名为Messages的集合。定义如下: var email = userEmail(); var time = new Date().getTime(); //value is the text of the message message = {user: Meteor.userId(), time: time, message: value}; Messages.insert(message); 在我的模板中,我有以下内容: {{#each messag
var email = userEmail();
var time = new Date().getTime();
//value is the text of the message
message = {user: Meteor.userId(), time: time, message: value};
Messages.insert(message);
在我的模板中,我有以下内容:
{{#each messages}}
<li class="panel panel-default">
<p class="date">{{time}}</p>
<a href="http://{{message}}">{{message}}</a>
</li>
{{/each}}
{{#每条消息}
{{time}
{{/每个}}
当然,当我访问我的网页时,它会将时间显示为历元时间戳编号,例如:1398954569368
我想处理我的{{time}}
,以便它以人类可读的格式显示它
如何或在何处访问{{time}
变量,以便对其执行JavaScript操作?此数字是Date().getTime()返回的数字。签出日期对象上的MDN:
Date.prototype.getTime()
返回指定值的数值
日期为自1970年1月1日00:00:00 UTC以来的毫秒数
(之前的时间为负数)
可以使用Template helper中的Moment.js访问/修改日期/时间对象,前提是可以通过该路径访问您的信息。例如:
Template.yourTemplateName.time = function() {
return moment(yourDateObject).fromNow();
};
我知道如何将历元时间戳转换为日期。请注意最后一行:
如何或在何处访问{{time}}变量,以便对其执行JavaScript操作?
。然后,特别是,在那里,我可以访问我错过了它的道歉。您可以在模板帮助器中访问/修改它,前提是您的信息可以通过该路径访问。例如:Template.yourTemplateName.time=function(){返回时刻(yourDateObject.fromNow()};啊,帮手。现在可以了。如果你在你的帖子中包括这一点,我会选择你的答案作为接受。