javascript - 从模板助手meteor/mongodb访问minimongo

标签 javascript mongodb meteor minimongo

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

相关文章:

javascript - 将用户输入添加到数组//Javascript

mongodb - 对象中的对象 对象中的对象 Golang

javascript - Meteor可以在不依赖外部服务的情况下运行吗?

javascript - 指定 Meteor 包依赖项的正确方法

Meteor:如果用户的电子邮件未经过验证,则阻止对应用程序的访问

javascript - 删除 Disqus 投票

javascript - 检测操作系统(XP 与 7)

javascript - 用切片复制数组时的奇怪行为

mongodb - 在 Azure 数据工厂中创建 MongoDB 链接服务

mongodb - mongo dbname --eval 'db.collection.find()' 不起作用