我正在尝试在 html 页面中访问 Minimongo 中的产品集合。当我在浏览器控制台中时,我可以输入 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
});
最佳答案
简单回答: 在辅助函数中对集合进行所需的任何修改,然后返回一个 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}
});
关于javascript - 从模板助手meteor/mongodb访问minimongo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29778919/