gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:09:27 25 4
gpt4 key购买 nike

我正在研究 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com