javascript - Getting data from mongodb/mongoose using predefined functions -


this how getting data mongodb:

users.get(base_url, (req, res) => {     usermodel.find({}, (err, docs) => {         res.render("users/index", {             title: "all users here",             user_list: docs         });     }); }); 

now, can see express application. like, simple call function can value docs variable inside mongodb model callback. how do this, ideally, want see this:

users.get(base_url, (req, res) => {     res.render('<some_jade_file_here>', {             title: "yes, got right",             user_list: getallusers();         }); }); 

ideally, want call function. how can this, since having put render inside of mongodb call problem, since may want query bunch of things database, , might not 1 database. i'm struggling little since i'm not used callbacks.

any appreciated. if you're wondering syntax () => {}, thats anonymous function in typescript.

you can't without callbacks, can use async flow control library async manage nest of callbacks. in case want use async.parallel.

using can like:

users.get(base_url, (req, res) => {     var data = {         title: "yes, got right"     };     async.parallel([         (callback) => {             usermodel.find({}, (err, docs) {                 data.user_list = docs;                 callback(err);             });         },         (callback) => {             // other query populates field in data         }     ], (err, results) => {         // called after parallel functions have called callback         res.render('<some_jade_file_here>', data);     }); }); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -