• Write to a nested schema nodejs mongoose using push

    abemassry        
    0 Likes0 Commentsjavascript

    app.post('/comment', function(req, res){
        if (req.user) {
          models.users.findOne({name: req.user.username}, function(err, userdata){
            var picture = userdata.photo;
            var now = new Date();
            code_id = req.param('code_id');
            models.codes.findById(code_id, function(err, code){
              if (err) return handleError(err);
              code.comments.push({user: req.user.username, user_photo: picture, body: req.param('comment'), date_commented: now});
              var doc = code.comments[0];
              console.log(doc);
              code.save(function(err) {
                console.log('error check');
                if(err) { throw err; }
                console.log('saved');
                res.redirect('/code/'+code_id);
              });
            });
          });
        }
      });
    

Comments (0)