extranting search results

9 views
Skip to first unread message

Angel Rodriguez

unread,
Jun 3, 2015, 10:52:56 AM6/3/15
to mongoo...@googlegroups.com
Hello. Is there any way to extract the results of a search for use in a rendering ??

Example: 

var query = dbPeople.find({}).lean().exec(function(err, data) {
    return data;
});

app.get('/', function(req, res, next) {
res.render('index', { 
data: query
});
});


Thanks !

Jason Crawford

unread,
Jun 3, 2015, 11:54:01 AM6/3/15
to mongoo...@googlegroups.com
The app.get() call is configuration/setup for your router. That is done at startup/boot time. You don't want to do database queries then. You want to do them in the course of processing a request. Something like this:

app.get('/', function(req, res, next) {
dbPeople.find({}).lean().exec(function(err, data) {
res.render('index', { 
data: data
});
});
});

Hope that helps,
Jason

--
Blog: http://blog.jasoncrawford.org  |  Twitter: @jasoncrawford



--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Angel Rodriguez

unread,
Jun 3, 2015, 12:19:56 PM6/3/15
to mongoo...@googlegroups.com
I understand that !!, But, what if I want to do a database query before to have some data ready for some requests ????

Thanks !  :-)

Jason Crawford

unread,
Jun 3, 2015, 12:41:53 PM6/3/15
to mongoo...@googlegroups.com
I suppose you could do a query on startup and cache the data. Not necessarily a good idea (what if the data changes?) But then you would have to pass the data itself to res.render()… in your code I think you're passing the promise for the data.
Reply all
Reply to author
Forward
0 new messages