简体   繁体   中英

Accessing minimongo from template helper meteor / mongodb

I am trying to access my Products collection in Minimongo in the html page. When I am in my browser console, I am able to type Products.findOne(); and it will return a product.

However, when I try to return a product from my template helper, I get undefined. Thoughts anyone?

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 

});

Simple answer: Do whatever modification you need to do on the collection within the helper function and then return a JS object. For instance if you collection looks something like this:

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

you can do the following to transform it

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}

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM