Mongodb Get Data to Display in HTML

Getting data from nodejs/MongoDB in a HTML page with Ajax

getCalendar is not an async function. Also it doesn't take any parameters.

You can change your code like this:

app.get("/", async function (req, res) {
try{
const calendar = await mymongo.getCalendar();
res.send(calendar);
} catch(e) {
//handle reject case
}
});

and mongo.js like this:

exports.getCalendar = () => {
return new Promise((resolve, reject) => {
Calendar.find(function (err, results) {
if(err){
reject(err);
}else{
resolve(results);
}
});
})
};

mongodb query data to html drop down

You cannot get the data from async function outside the scope of that function itself. You can read this answer on SO to better understand how async calls work.

That's why console.log(dbCountries) always return the promise.

I would suggest you to use async/await and modify your GET route

app.get('/', async (req, res) => {
console.log(req.url);
// weather info / data -> display set to on
let weatherVisibilty = 'visibility_on';

let dbCountries = await mongodbActions.mongodbGetCountries();
let countryList = dbCountries

res.render(index, {
countryList: countryList,
setVisibility: weatherVisibilty
});
})


Related Topics



Leave a reply



Submit