0

Here is the code

app.get('/home' , async(req, res) => {
    let totalEmployees = Employee.find();
    totalEmployees.count(function (err,totalWorkers) {
        let total = totalWorkers
    })
    res.render('pages/home' , {total})
})

It does not allow me to use ejs to display the total variable as its undefined cause its out of the scope and exists only in the function , how do i make it accessible outside the scope

  • my guess it is on the lines of https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call but I have no clue what find or count are. – epascarello Dec 27 '21 at 19:32

1 Answers1

0

You can define your variable above your function in global. In that way you only have to import the file in which the var is defined. Another option is to return the var so you can take it from whereever you call the function.