【发布时间】:2016-12-07 11:51:34
【问题描述】:
如何使用文档 userId 检索文档单个属性值?
首先我使用以下代码返回文档_Id
Session.set('getUserId', Al.findOne({_id: Session.get('appealId')}));
console.log(Session.get('getUserId').userID);
我可以看到userID 在控制台中打印成功,现在我想使用userID 从Meteor.users 集合中检索另一个值。
这是我的代码:
Session.set('userDetails', Meteor.users.findOne({_id: Session.get('getUserId').userID}));
console.log(Session.get('userDetails').profile.annualLeave);
但它似乎不起作用,我在控制台中收到以下错误:
TypeError: Cannot read property 'profile' of undefined
我尝试将Session.get('userDetails').profile.annualLeave 更改为Session.get('userDetails').username,但我仍然收到同样的错误,告诉我username 未定义
更新:
对于appealId 会话,我使用它来存储来自路由器的项目的_Id。稍后使用它来检索userID(项目内的属性)。
代码如下:
Router.route('/appeal/:_id', function(){
Session.set("appealId", this.params._id);
this.render("navbar", {to:"navbar"});
this.render("appealDetails", {to:"main"});
});
【问题讨论】: