【问题标题】:meteor - get value from collection serverside流星 - 从收集服务器端获取价值
【发布时间】:2015-08-07 17:00:49
【问题描述】:

我想从 current_user(client) 向 userY(a member) 发送一封电子邮件,而不会将电子邮件地址暴露给客户端。所以所有的服务器端。

我有 userY 的 _id(来自路由器参数:email.toUser = Router.current().params._id;)并将其作为值发送给方法。

在方法函数中我想做类似的事情

var to = Meteor.users.find({ _id: email.toUser }); 

现在当我 console.log(to) 我得到一个巨大的 _mongo 对象而不是用户配置文件(我希望能够记录:to.profile.email)什么是从电子邮件字段中获取值的最佳方式?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您看到的是集合集的光标。

    要检索记录,您可以使用.fetch()

    var to = Meteor.users.find({ _id: email.toUser }).fetch();
    console.log(to[0].profile.email);
    

    当您通过 ID 查找时,您只需要 1 个结果,因此您也可以使用 findOne() 而不是 find() 这将直接返回第一个元素。

    var to = Meteor.users.findOne({ _id: email.toUser });
    console.log(to.profile.email);
    

    编辑:我想补充一点,用email.toUser 替换{ _id: email.toUser } 应该可以。仅使用 ID 时,无需传递对象。

    【讨论】:

      【解决方案2】:

      您应该将find 更改为findOne

      像这样。

      var to = Meteor.users.findOne({ _id: email.toUser }); 
      

      找到返回整个Mongo.Collection光标

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-18
        • 2014-09-13
        • 2018-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        相关资源
        最近更新 更多