What is wrong with this result? my res.send(dbResult) is not giving me any data. It is empty.
/* GET users listing. */
router.get('/', function(req, res, next) {
async function getData(){
try{
var dbResult = await mongodb.findData("contacts","iyaiContacts");
console.log('before dbResult');
res.send (dbResult);
}catch(error){
console.log("error getting contacts...", error.message);
}
}
getData();
});
Additional code for clarity. I have a db.js file that is used to handle the MongoDB connections and queries. Here is the Find Data function. Sorry I didn't realize I was being confusing until people asked questions.
const findData = (myColl, myDb) => {
try {
MongoClient.connect(
url,
function(err, client) {
client
.db(myDb)
.collection(myColl)
.find()
.sort({ nmFull: 1 })
.toArray(function(err, result) {
console.log("find() result...", result);
return result;
});
}
);
} catch (e) {
console.log(`findData() error message is...${e.message}`);
client.close(); // trying to close open db connection if it exist
throw e;
}
};