javascript - Mongodb - 为什么我的 Users.findOne 未定义?

标签 javascript mongodb meteor

我正在研究 Meteor,试图从 Mongodb 集合中找到一些值。 这是代码:

      var sameLogins = Users.findOne({login: 'a'});
      console.log(sameLogins);

但它正在返回并且“未定义”。 但记录存在于集合中: enter image description here

那么,有人能告诉我我错过了什么吗?

此外,在 mongo 控制台中 - 一切正常: enter image description here

我正在寻找发布/订阅的内容,但我正在使用自动发布模块。

谢谢!

最佳答案

我会将这个问题的答案留给有同样问题的新用户。

如果您使用 autopublish 包,那么您应该知道它正在为每个集合发布 .find() 的结果。

但是,默认情况下,Meteor.users.find() 将仅返回 _idprofile 字段,因此您的 中的文档code>Meteor.users client 集合将仅包含这两个字段。

最简单的解决方法是创建您自己的出版物(例如 allUsers)并在其中返回您需要的字段:

服务器:

Meteor.publish('allUsers', () => {
  // check for Meteor.userId() is omitted, put it here, if needed
  return Meteor.users.find({}, { fields: { ... } });
});

不要忘记订阅它:

客户:

Meteor.subscribe('allUsers');

关于javascript - Mongodb - 为什么我的 Users.findOne 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943810/

相关文章:

javascript - Highcharts addPoint 函数什么也不显示

node.js - 按日期时间字段获取最新的 MongoDB 记录

node.js - Mongo 和 Handlebars

facebook - Meteor:Facebook 用户名不显示

javascript - 有没有办法模拟请求对象的属性

javascript - SoundManager2 WaveFormData 和 PeakData 返回 0

javascript - Video.js 在视频上附加黑色边框

node.js - Nodejs 应用程序错误 : bind EADDRINUSE when use pm2 deploy

javascript - 根据 Meteor.js 中的变量值更改 html

javascript - meteor 出版领域问题