Passing req object to Schema defaults

20 views
Skip to first unread message

Illimar Tambek

unread,
Mar 25, 2013, 5:05:24 PM3/25/13
to mongoo...@googlegroups.com
I am trying to set some default values for a document by fetching them from the settings of the current user, which is available in the req.user object.

I know that it's possible to pass the req object to middleware, but is it possible to pass it to the Schema default functions? Something like:

var Comment = new Schema({
  mood:{ type: String, default: function(req) {
    return (req.user.defaults.mood);
  }},
  content: String
});

Aaron Heckmann

unread,
Mar 27, 2013, 12:37:20 AM3/27/13
to mongoo...@googlegroups.com
It is not. You could wrap document construction in a custom function:

function makeShinyThing (req) {
  var doc = new ShinyThing;
  doc.brightness = req.params.brightness * 1000;
  return doc;
}


--
--
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 ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en
---
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/groups/opt_out.
 
 



--
Aaron


Reply all
Reply to author
Forward
0 new messages