控制台返回未定义 [英] Console returning undefined

查看:45
本文介绍了控制台返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在控制台中写这个时,我得到了未定义但是数据在数据库中.我错过了什么?

When I write this in console I get undefined however the data is in the database. What am I missing?

Meteor.users.findOne({_id: this.userId},{fields: {"profile.pastEmployer.name": 1}});

推荐答案

我想你可能想要做的是:

I think what you may want to do is this:

var pastEmployerName = Meteor.user().profile.pastEmployer.name;

根据您对这些嵌套属性存在的信心程度,您可能需要使用 保护 像这样:

Depending on how confident you are in the existence of those nested properties, you may want to use a guard like so:

var profile = Meteor.user().profile;
var pastEmployerName = profile && profile.pastEmployer && profile.pastEmployer.name;

注意事项:

  1. 使用Meteor.userId()获取当前用户的id,Meteor.user()获取当前用户的文档.在发布者和方法中,我们使用 this.userId.

  1. Use Meteor.userId() to get the current user's id, and Meteor.user() to the the current user's document. In publishers and methods, we use this.userId.

字段投影(在您的原始问题中使用)为您提供一个包含 _id 和显示指定字段的最小结构的对象.在您的情况下,您希望获得一个带有 _idprofile 的对象,该对象又包含一个 pastEmployer 等等.一般来说,字段投影在服务器上是有益的(它们节省带宽和 CPU),但在客户端上的使用有限,因为完整的文档已经在内存中.

A fields projection (as used in your original question) gives you an object including an _id and the minimal structure to display the specified field(s). In your case, you'd expect to get an object with an _id and a profile, which in turn contains a pastEmployer and so on. In general, fields projections are beneficial on the server (they save on bandwidth and CPU), but are of limited use on the client because the complete documents are already in memory.

这篇关于控制台返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆