Generate an array from a query in mongoDB (Mongoose)

Asked

Viewed 192 times

0

Good morning! I need the data from a collection of Mongo on the front end.

I’m using Mongoose as ODM, and I got a little lost in sending the data to the front end.

In the query it returns me the documents of the collection, and then I need them in JSON (I believe) to use them in the front end to generate a graph.

That’s the route I’m using

router.get('/home', isAuthenticated, function(req, res){

    atendimentos.find({}, async function(err, atendimentos) {
         if(!err){
              console.log(atendimentos);
         }
    })

    res.render('home', {user: req.user, atendimentos: atendimentos});
})

I saw that I could use Lean(), but it returns pure javascript objects from what I understood. I could use it and use a JSON.stringify I imagine, but then on the route I can not use two res, otherwise the application crasha.

I would like to know how I could do to have this data on the front end, if anyone can give me a light, thank you very much!

1 answer

0


To pass the data and receive on the front.

router.get('/home', isAuthenticated, function(req, res){
    atendimento.find({}, (err, result) => {
        if(!err){
            res.render('home', { result })
        }else{
            console.log(err)
        }            
})

And using ejs as a view engine ai to gain access to the data is very simple only using this syntax <% = atendimento.name %> or <%= user %>

Async/await

router.get('/home', isAuthenticated, async (req, res) =>{
    const result = await atendimento.find({})
    res.render('home', { result })           
})
  • I need the service data on the front, his model in Mongo has 13 fields if I’m not mistaken, but I only need 4 (I don’t know if this is relevant, but I know how to filter it, no problem). I guess that’s the way you said it!

  • oks, anything just talk.

  • I tried something like this: const data = {calls} because the user already has and works well, then I passed the data like this: res.render('home', {user: req.user, chartData: data}, but then in the chart gave that chartData is Undefined.

  • But then it’s the same thing you did before, difference that added another layer to the data, I think it gets worse like this. Could you give me more information, how you’re manipulating the data on the front?

  • I have this home view there, there I have a script that has the part of Chartjs that I want to use to render the graph from the bank. There is a date field that is where the dice go, then I tried to pass this chartData there and did not roll. To half lost.

  • So, you could tell me if you’re using some engine for the view?

  • Expressjs, . ejs is my view engine

  • Great, is the script imported on the right page? If it is charData is its variable there, from a <%=JSON.stringify(chartData)%> anywhere on the page just to see if the data is inside. Using that render yours here res.render('home', {user: req.user, chartData: data}

  • If it’s the Chart script, yes. I’ll look here, we’ll test it. Anything we go to chat because here it’s getting long.

Show 5 more comments

Browser other questions tagged

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