如何在 meteor 变量上工作并遇到 miniMongo 问题

How to work on meteor Variable & Having an issue with miniMongo

我是 meteor.js 的新手。

在我正在开发的应用程序中,用户可以 post 网站上的东西,用户可以评论它,就像在社交网络上一样。然后我有:

{{#each posts}}
  {{>onePost}}
{{/each}}

在我的 onePost 模板中,我打印了 onePost 文本及其发布日期。但是由于 new Date(); 在末尾添加了 GMT 数据,mongoDB 中添加了日期。

我想知道我能否将字符串缩短为 x 字符。我知道在 javascript 中,我们必须使用 .substring(int, int) methods,但我如何将这样的成员应用到像这样的流星变量上?

<h6>date</h6>

as none 以下似乎有效:

<h6>{{date}}.substring(0,3);</h6>
<h6>{{date.substring(0,3);}}</h6>

我确实遇到了另一个麻烦,因为我正在测试,我在控制台上调用了一个方法来清空我的 posts collection 并清除我的页面。 但从那以后,我无法再次输入文字。当我验证我的 post 时,我只看到 [object Object]

这是我要插入 collection 的代码:

Template.post.events({
    'keyup .comment':function(evt,tmpl){
        if(evt.which === 13 && !evt.shiftKey){
            var commenttext = tmpl.find('.comment').value;
            var options = {text:commenttext,parent:this._id};
            Meteor.call('addPost',options);
            $('.comment').val('').select().focus();
        }
    }
})

这是我处理 collection 的方式:

Posts = new Mongo.Collection('posts');

Meteor.methods({
//{text:'',owner:'',date:'',parent:''}
'addPost':function(options){
    var post = {
        text:options.text,
        owner:Meteor.userId(),
        date:new Date(),
        parent:options.parent
    }
    Posts.insert(post);
},
'removePost':function(id){
    Posts.remove({_id:id});
},
'removeAllPosts':function(){
    Posts.remove({});
}

})

因为我调用了 Meteor.call('removeAllPosts');在 Firefix js 控制台中,我 post 的所有内容都有文本:[object Object]

我认为,出于某种我不知道的原因,它现在将查询的 return 视为实例列表,但由于它正确显示了作者姓名,我宁愿认为它来自 collection 中的插入。但是我没有用nsertion函数改变任何东西。

可以在您的助手中这样做:

{{date.toString.substring 0 3}}

但是,我建议改用助手:

Template.myTemplate.helpers({
  formattedDate: function() {
    return this.date.toString().substring(0, 3);
  }
});
{{#each posts}}
  <h6>{{formattedDate}}</h6>
  {{>onePost}}
{{/each}}

或者如果你想让它更通用:

Template.myTemplate.helpers({
  shorten: function(data) {
    return data.toString().substring(0, 3);
  }
});
{{#each posts}}
  <h6>{{shorten date}}</h6>
  {{>onePost}}
{{/each}}