B
B
Bernard Krapivin2017-08-29 20:10:35
JavaScript
Bernard Krapivin, 2017-08-29 20:10:35

MongoDB - Why is Users.findOne undefined?

Hello!
With this code, I want to find an entry in a collection:

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

But, "undefined" is returned to me, although there is an entry in the collection:
F3kxU.png
Please tell me what is the problem?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Bernard Krapivin, 2017-08-29
@Bobrodon

The code above was needed to check user logins for uniqueness.
As it turns out, the meteor has built-in tools for this.
I figured it out thanks to this documentary:
https://docs.meteor.com/api/passwords.html#Account...
This is what the code looks like:
b91b30d009.png

A
Alexander, 2017-08-29
@boratsagdiev

Because Users.findOne is an asynchronous method and the result should be output in a callback or a promise?

var sameLogins = Users.findOne({ login: "a" }, (err, user) => {
  if (err) console.log(err);

  console.log(user);
});

With promises:
var sameLogins = Users.findOne({ login: "a" })
  .then(user => console.log(user))
  .catch(err => console.log(err));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question