'My database query function always return null
I maked query function for my project. But result always is null, how i can debug this code?
const queryDB = async (collection,filter) => {
let result = null
await MongoClient.connect(url, function(err, getDb) {
if (err) throw err;
const db = getDb.db("global");
db.collection(collection).findOne(filter, function(err, result) {
if (err) throw err;
result = result
})
})
return result
}
Solution 1:[1]
Maybe this works
queryDB = (collection, filter) => {
await client.connect();
const db = client.db(dbName);
return db.collection(collection).findOne(filter);
}
queryDB("collection", "filter").then((err, result) => {
if(err) throw err;
console.log(result)
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Emrah |
