node.js - How to set creator for this model mongoose -
i have create social network twitter people post tweet has comment in it. defined
var comment = new schema(); comment.add({ title : { type: string, index: true } , date : date , body : string , created_by: { type: schema.types.objectid, ref: 'user' } }); var tweetschema = new schema ({ created_at : { type: date, default: date.now } , created_by: { type: schema.types.objectid, ref: 'user' } , title : string , comments : [comment] , body : string , picture: [url] }) because write mobile application request through rest api provide wanna inquire when create tweet can send server have creator_info , comment have read here how populate sub-document in mongoose after creating it? dont know how write method create tweet or comment , set creator this. give thanks advance.
you can do. before saving tweet, seek create user data. if fails, pass error. if not, set created_by field user's id.
look here more info: http://mongoosejs.com/docs/middleware.html
tweetschema.pre('save', function(next) { var user = new user(this.created_by, function(err, user) { if (err) homecoming next(err); this.created_by = user._id; next(); }); }); node.js mongoose
No comments:
Post a Comment