node.js - Mongoose create with push for multiple refs -


so have kind of nested structure db here 3 schemas : user schema:

var userschema = new mongoose.schema({      email: {         type: string,         lowercase: true,         unique: true,         required: true     },     password: {         type: string,         required: true     },     role: {         type: string,         enum: ['trainer', 'client', 'admin'],         default: 'client'     },     trainer: {         type: mongoose.schema.types.objectid,          ref: 'user'     },     programs:  [        {                 type: mongoose.schema.types.objectid,                  ref: 'program'             }         ],     diet:   [     {         calories: {             type: number         },         protein: {             type: number         },         fats: {             type: number         },         carbs: {             type: number         },         fiber: {             type: number         }     }],     stats:         {           bodyweight:                [ {                   _id: false,                   measurement:{ type: number},                  time : { type : date, default: date.now }               }               ]         },     name: {         type: string,         required: true     },  }, {     timestamps: true }); 

program schema:

var programschema = new mongoose.schema({      title: {type: string, required: true},     description: {type: string, required: true},     createdby: {         type: mongoose.schema.types.objectid,          ref: 'user'     },      exercises: [         {             exercise:{                 type: mongoose.schema.types.objectid,                  ref: 'exercise'             },             sets:{                 type: number             }         }          ] }, {     timestamps: true }); 

and exercise schema:

var exerciseschema = new mongoose.schema({      name: {         type: string,         lowercase: true,         unique: true,         required: true     },     description: {         type: string,         required: true     },     video: {         type: string     },     image: {         type: string     }  }, {     timestamps: true }); 

as can see , user can have many programs , program can have many exercises. when return user object, using populate function, exercises within users programs not populated. beleive down how create program, understanding have push exercise objects program, i'm not sure how create program , push exercises @ same time. current create program program function:

exports.createprogram = function(req, res, next){      program.create({         title : req.body.title,         description: req.body.description,         exercises: req.body.exercises,         createdby: req.body.createdby      }, function(err, program) {          if (err){             res.send(err);         }          program.find(function(err, programs) {              if (err){                 res.send(err);             }              res.json(programs);          });      });  } 

is there way can kinda of foreach on req.body.exercises maybe? in advance here how return users:

exports.getclients = function(req, res, next){          var trainerid = req.params.trainerid;      user.find({trainer: trainerid})         .populate({             path: 'programs',             populate: { path: 'exercises' }         })         .exec(function (err, clients) {             if (err) return handleerror(err);         console.log(clients);         res.json(clients);         });  } 

i think on right path, have populated exercise wrong. instead of populating exercises, need populate exercises.exercise.

try this:

user.find({trainer: trainerid})     .populate({         path: 'programs',         populate: { path: 'exercises.exercise' }     })     .exec(function (err, clients) {         if (err) return handleerror(err);     console.log(clients);     res.json(clients);     }); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -