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!
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!
– danibrum
oks, anything just talk.
– Chance
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.
– danibrum
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?
– Chance
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.
– danibrum
So, you could tell me if you’re using some engine for the view?
– Chance
Expressjs, . ejs is my view engine
– danibrum
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 hereres.render('home', {user: req.user, chartData: data}
– Chance
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.
– danibrum
Let’s go continue this discussion in chat.
– danibrum