I am making a website where i need to import data from database and use it later in program. But when i am using npm package mysql
i can't find any way to import result of query outside function for later use. Here is an example of my code.
Function with that i get data from database:
let output;
dba.query('SELECT * FROM `movies-web`.movies;',(err, rows) => {
if(err) {
throw err;
} else {
output = rows;
}
});
And in code i use it like this:
res.write(output);
But the variable output
return undefined
.
I am solving this problem for longer than month, i browsed so many pages, questions here on stackoverflow and i can't find working solution.