2
I want to use the data that return from Mongodb’s 'find()' to, through a foreach, create a table using the array that returns from Mongodb, and after the table is ready, generate a PDF, but I am not able to access the result data outside the connection function with DB.
db.open( function (err, mongoclient) {
mongoclient.collection('postagens', function (err, collection) {
collection.find().toArray(function(err, results){
if(err){
res.json(err);
} else {
res.send(results);
}
mongoclient.close();
return results
})
})
})
let conteudoPDF = {
content:[
{text: 'Teste de PDF', style: 'header'},
'Teste com pdfMake',
{
table:{
body:[
[ 'Id Usuário', 'Id Post', 'Título', 'Texto'],
/*Aqui irá o forEach para gerar a tabela*/
]
}
}
]
}
The 'Results' variable contains the JSON array I need, and I want to have access to this data outside the DB connection function, and 'Return' is not exporting this data outside the function
I indicated as duplicate because the problem is the same, only changes the way to apply, who look here will be able to see more explanations in the other answers.
– BrTkCa