从模板助手meteor / mongodb访问minimongo

时间:2015-04-21 17:13:27

标签: javascript mongodb meteor minimongo

我正在尝试在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 

});

1 个答案:

答案 0 :(得分:1)

简单回答: 在helper函数中对集合执行任何修改,然后返回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}

});
相关问题