[英]Accessing minimongo from template helper meteor / mongodb
我正在嘗試在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
});
簡單的答案:對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}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.