0

First, here is the code:

Mongoose Schema

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

// Registration Form schema
var RegistrationFormSchema = new Schema;

RegistrationFormSchema.add({
  studentFirst: { type: String, required: true },
  studentLast: { type: String, required: true },
  studentGrade: { type: String, required: true },
  guardianFirst: { type: String, required: true },
  guardianLast: { type: String },
  guardianEmail: { type: String },
  courseString: { type: String },
  courseArray: { type: Array }
});

module.exports = mongoose.model('RegistrationForm', RegistrationFormSchema);

Relevant API Route

  apiRouter.route('/registerNow')
    .post(function(req, res) {

      var newForm = new RegistrationForm();
      console.log(req.body);
      newForm.studentFirst = req.body.studentFirst;
      newForm.studentLast = req.body.studentLast;
      newForm.studentGrade = req.body.studentGrade;
      newForm.guardianFirst = req.body.guardianFirst;
      newForm.guardianLast = req.body.guardianLast;
      newForm.guardianEmail = req.body.guardianEmail;
      newForm.courseString = req.body.courseString;

      newForm.save();
  }

And this is inside of my controller, where I am making the request. Also making a request in Postman with the same response, so not sure if it is anything to do with how I am requesting, but want to include this to have more info for an answer.

$http.post('/api/registerNow', {
  'studentFirst': vm.courseInfo.studentFirst,
  'studentLast': vm.courseInfo.studentLast,
  'studentGrade': vm.courseInfo.studentGrade,
  'guardianFirst': vm.courseInfo.guardianFirst,
  'guardianLast': vm.courseInfo.guardianLast,
  'guardianEmail': vm.courseInfo.guardianEmail,
  'courseString': vm.courseInfo.courseString,
  'courseArray': ['asdasd', 'fasfasf', 'gasgasga']
});

Now that we have the relevant code, the question!

When I make my HTTP Post request to save to my database, it all saves, but it saves courseArray as an empty array '[]' no matter what, if it is empty or full.

I assume it's a Mongoose Schema issue.

1 Answer 1

1

In your route you're not setting the newForm.courseArray property with the data from the request. Mongoose is probably setting the default value as an empty array.

Try using the following code

apiRouter.route('/registerNow')
    .post(function(req, res) {

        var newForm = new RegistrationForm();
        console.log(req.body);
        newForm.studentFirst = req.body.studentFirst;
        newForm.studentLast = req.body.studentLast;
        newForm.studentGrade = req.body.studentGrade;
        newForm.guardianFirst = req.body.guardianFirst;
        newForm.guardianLast = req.body.guardianLast;
        newForm.guardianEmail = req.body.guardianEmail;
        newForm.courseString = req.body.courseString;
        newForm.courseArray = req.body.courseArray; //This line is missing

        newForm.save();
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.