Answer the question
In order to leave comments, you need to log in
Why does async await return undefined as a result of a database query?
Below is a function that would have to find a user in the database and return it.
If I understand correctly, then MongoClient.connect returns null, but if I returned the user in the callback, can I get it in the resp variable ?
In turn, in function log returns null or the user to me, and resp returns undefined.
Am I doing something wrong with async/await, or should I return the user otherwise?
Essentially, I have to check the login and pass in this function, but it must return something.
Please tell me how to verify the login and pass.
async function checkIfUserExists(user) {
const resp = await MongoClient.connect(mongoUrl,
{
useNewUrlParser: true,
useUnifiedTopology: true
},
async function(err, db) {
if (err) throw err;
let dbo = await db.db("todo");
let user = await dbo.collection("users").findOne(user);
console.log('in function log');
console.log(user);
return user;
});
console.log('resp');
console.log(resp);
return resp;
}
Answer the question
In order to leave comments, you need to log in
Shaw is some kind of crap, there is a callback for the connection if there are no promises. I haven't looked at their lib for a long time, but it returns a promise if there is no callback everywhere, as far as I remember.
const db = await MongoClient.connect(mongoUrl)
let dbo = await db.db("todo");
let user = await dbo.collection("users").findOne(user);
console.log(user);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question