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
Want to Show/Hide Div Based on Dropdown Box Selection
How to Access the Value of a Promise
How to Get the Contextpath from Javascript, the Right Way
How to Reference Static Assets Within Vue JavaScript
Generate Child Row Under a Parent Row in HTML Table
How to Load Content of Page Without Refreshing the Whole Page
Resize a Base-64 Image in JavaScript Without Using Canvas
Add Parameters to Url Without Refresh
Trigger JavaScript Event If Div Innertext Change
How to Disable Eslint Rule Max Line Length for Paragraph in <Template> of Vue.Js
Jquery Get Closest Td Text Value
How to Redirect Url After Login Successful Via Ajax Call in PHP
Combine Multiple JavaScript Files into One Js File
Textarea With Initial Auto Height by Content With Pure CSS
Connecting to Tcp Socket from Browser Using JavaScript