0

I'm working on Meteor, trying to find some values from Mongodb collection. here is the code:

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

But it's returning and "undefined". But record exists in collection: enter image description here

So, can anybody tell what I'm missing?

Also, in mongo console - everything is working fine: enter image description here

I was looking in Publish/Subsribe stuff, but i'm using autopublish module yet.

Thank you!

Oleg Smirnoff
  • 111
  • 1
  • 9
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – alexmac Aug 29 '17 at 16:15
  • 1
    You need to provide a callback. – evolutionxbox Aug 29 '17 at 16:17
  • If you are using the user collection from the package `account-base` then you can access it with `Meteor.users` and not `Users`. `Meteor.users.findOne({login: 'a'});` – Gaëtan Rouziès Aug 29 '17 at 16:20
  • @evolutionxbox how could I make it? – Oleg Smirnoff Aug 29 '17 at 16:34
  • 1
    You should show the code where you're initializing your `Users` as well, otherwise it's unclear what is it. – Styx Aug 29 '17 at 16:42
  • @Styx they are: `export const Users = Meteor.users;` – Oleg Smirnoff Aug 29 '17 at 16:52
  • @OlegSmirnoff did you try to use `Meteor.users.findOne(...)` instead of `Users.findOne(...)`? – Styx Aug 29 '17 at 17:01
  • @Styx yes, I partly understand what's the problem, there are no callback, but I'm new at programming, so I don't understand it a bit :D – Oleg Smirnoff Aug 29 '17 at 17:03
  • @OlegSmirnoff I don't think your problem is related with callbacks. In Meteor, DB calls are synchronous. Have a look at the [`findOne` documentation](https://docs.meteor.com/api/collections.html#Mongo-Collection-findOne). – Styx Aug 29 '17 at 17:12
  • @OlegSmirnoff I suspect you and your Meteor app are using different Mongo DBs, that's why you're getting different results. Could you confirm/deny that? – Styx Aug 29 '17 at 17:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153156/discussion-between-oleg-smirnoff-and-styx). – Oleg Smirnoff Aug 29 '17 at 17:31

2 Answers2

2

I will leave the answer for this issue for new users having the same problem.

If you're using autopublish package then you should be aware that it's publishing the result of .find() for every collection.

But, Meteor.users.find(), be default, will return only _id and profile fields, so documents in your Meteor.users client collection will have these two fields only.

The most easy workaround for this would be to create your own publication (allUsers, for example) and in it to return those fields you need:

Server:

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

Don't forget to subscribe to it:

Client:

Meteor.subscribe('allUsers');
Styx
  • 9,863
  • 8
  • 43
  • 53
0

Update for Meteor: Right now you are storing a cursor in your variable sameLogins. In order to retrieve the results you want, you must actually execute this query by either calling fetch(). What is returned from findOne without fetch is essentially an object that you could use to iterate over and find mongoDB documents - (called a collection cursor). The cursor is not your result itself.

Calling fetch would like something like:

Users.findOne({login: 'a'}).fetch()
pneedle
  • 9
  • 2
  • Why do you think the `mongoose` is used? There is no such thing mentioned, neither in tags nor in the question itself. – Styx Aug 29 '17 at 17:15
  • @pneedle Anyway, you can join chat, to help with this issue http://chat.stackoverflow.com/rooms/153156/discussion-between-oleg-smirnoff-and-styx – Oleg Smirnoff Aug 29 '17 at 17:34