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
Post a Comment