0

我正在尝试在 html 页面的 Minimongo 中访问我的 Products 集合。当我在浏览器控制台中时,我可以输入Products.findOne(); ,它会返回一个产品。

但是,当我尝试从模板帮助程序返回产品时,我得到了未定义。有人想吗?

Template.Tires.onRendered(function() {
 console.log(Products.findOne());
 //after I return a product item, I need to modify its properties manually after it has loaded into the client 

});
4

1 回答 1

1

简单的回答:在辅助函数中对集合进行任何修改,然后返回一个 JS 对象。例如,如果您的收藏看起来像这样:

SomeColleciton
  _id
    type: String
  birthday:
    type: Date
  firstname:
    type: String
  lastname:
    type: String
  timezone:
    type: Integer

您可以执行以下操作来转换它

Template.Tires.helpers({
  user: function(userId) {
    var u = SomeCollection.findOne(userId);
    var age = calcAge(u.birthday);
    var ifworkinghour = calcifworkinghour(u.timezone);
    return {name: u.firstname + ' ' + u.lastname, age: age, workinghour: ifworkinghour}

});
于 2015-04-21T21:30:32.897 回答