express - pass options to mongoose schema toJSON transform inline (in expressjs)? -


i have mongoose (3.1) 'thing' schema tojson can customize in following manner...

thing.options.tojson = {}; thing.options.tojson.transform = function (doc, ret, options){   // ret, depending on options                                                                                                                                                                                 } 

as noted in code comment, change json representation given value of options. pass these options in expressjs action, maybe...

app.get(..., function (req ,res){   thing.find({}, function(err, things){     var myoptions = {...} // application stateful     return response.send(things) // maybe add options here?   }); }); 

how modify expressjs allow me supply options?

thanks,

g

you pass options in route handler passing them schema options:

app.get(..., function (req ,res){   thing.find({}, function(err, things){     thing.schema.options.tojson.myoptions = {...} // application stateful      return response.send(things) // maybe add options here?   }); }); 

this way, options available in transform function property of options object:

thing.options.tojson.transform = function (doc, ret, options){   console.log(options.myoptions); // prints app specific data provided earlier                                                                                                                                                                            } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -