【问题标题】:How to retrieve a single attribute value in a collection based on the document id?如何根据文档 id 检索集合中的单个属性值?
【发布时间】:2016-12-07 11:51:34
【问题描述】:

如何使用文档 userId 检索文档单个属性值?

首先我使用以下代码返回文档_Id

Session.set('getUserId', Al.findOne({_id: Session.get('appealId')}));
console.log(Session.get('getUserId').userID);

我可以看到userID 在控制台中打印成功,现在我想使用userIDMeteor.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"});
});

【问题讨论】:

    标签: meteor meteor-accounts


    【解决方案1】:

    错误信息的意思是

    Session.get('userDetails')
    

    没有定义,这反过来意味着对

    的调用
    Meteor.users.findOne({_id: Session.get('getUserId').userID})
    

    一定没有返回任何东西

    您不清楚这两段代码是如何关联的,而且您对 Session 的重复使用似乎令人费解。我什至不知道为什么会有这行代码

    console.log(Session.get('test').userID);
    

    除非“测试”是您之前存储的内容

    【讨论】:

    • 感谢您的纠正,我已修复它,我的意思是“getUserId”会话。再次抱歉
    • 好的,但你仍然没有接近根本原因。 Session.get('appealId') 来自哪里?
    • 我使用路由器存储它的值。我更新了我的问题:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多