How to use data from a query in mongoDB?

Asked

Viewed 383 times

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.

1 answer

1


You can fill in the contents of the PDF by invoking a function for this in the callback successful consultation, thus:

 db.open(function(err, mongoclient) {
    mongoclient.collection('postagens', function(err, collection) {
        collection.find().toArray(function(err, results) {
            if (err) {
                res.json(err);
            } else {
                // invocar a função que preenche o pdf aqui
                preencherPDF(results);
                res.send(results);
            }
            mongoclient.close();
            return results
        })

    })
})

function preencherPDF(conteudo) {
    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*/
                        conteudo.forEach((item) => {
                            console.log(item)
                        });
                    ]
                }
            }
        ]
    }
}
  • I’ll test here and inform the result...

  • If let conteudoPDF need to be read outside the function scope, declare out @Leonardoebert

  • Everything worked perfectly the way you made the code, the problem now will be to adapt JSON to the structure of the pdfMake library, but thank you very much for your help, I am beginner in web programming and was already with this same problem 2 days ago, thank you very much.

  • You’re welcome to @Leonardoebert. If you need help adapting JSON, we’d love to ask you a new question =]

  • Yeah, I ran into that difficulty now...

  • I’ve asked a new question

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.