Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Ryan Morales
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ryan Morales
Full Stack JavaScript Techdegree Graduate 33,933 Points

How do I add data to existing user in mongoose schema from a running express app - Dave Macfarland authentication course

I completed Dave's authentication with express and mongo course and I wanted to continue with the project by adding info to an existing user but I'm stuck. After registering a user, I want the logged in user to be able to answer questions and those answers be added to their schema, then displayed on their profile page. I cannot figure it out. Here is some of the code

//models/user.js
var UserSchema = new mongoose.Schema({
    email: {
      type: String,
      unique: true,
      required: true,
      trim: true
    },
    userName: {
      type: String,
      required: true,
      trim: true
    },
    name: {
      type: String,
      required: true,
      trim: true
    },
    favoriteBook: {
      type: String,
      required: true,
      trim: true
    },
    password: {
      type: String,
      required: true
    }
});
var User = mongoose.model('User', UserSchema);
module.exports = User;


// index.js
// GET /user-survey
router.get('/user-survey', function(req, res, next) {
  return res.render('user-survey', { title: 'User Survey' });
});

// POST /user-survey
router.post('/user-survey', function(req, res, next) {
  /*

  User.update(query, { surveyanswer1: 'Some personal information' };
  */

Thanks!

Hey Ryan Morales, I got your question and if you want to implement a functionality to make logged in users able to answer the questions available in the page and then display all the question with answer in a user profile.

So here are the steps for this :

Make user answer the question only when they are logged in.So you can create a middleware for this.

Add another property in your schema i.e. questions.Look something like this

              questions        : [{
                         question : {type:String,trim:true},
                                             answer   : {type:String,trim:true}
                    }]

And when user submitting the answer you can easily find the user in the database and update the record.

function(data,callback){
            User.findOneAndUpdate(
                {_id:req.user._id},
                {$push:
                    { 
                        questions : {
                                question : data.question,
                                                    answer   : data.answer
                             }
                    }
                },
                (err,user) => {
                    if(err) res.send({msg:"Something went wrong" ,success:false});
                    if(user) res.send({msg:"Data Saved",post:data,success:true});
                }
            );
        }

When someone make request to profile page find the user and get all the fields you want to display and print those details on a page.