2

I am trying to save this data in my mongodb database:

var Student_Answers = {
  matricNumber: matric,
  Answers:  answers  //This is the array answers = ["A", "B"]
};

This is my schema:

var Student_AnswersSchema = mongoose.Schema({
  matricNumber: String,
  Answers: [
    {
      type: String
    }
  ]
});

And this is where the save function is fired

var save = SA(req.body).save(function(err, data) {
  if (err) throw err;
  res.json(data);
});

The matric appears in the database and also the array name but never the contents of the array

enter image description here

11
  • you are saving ``` Answers: answers //This is the array ["A", "B"] ``` is array needs to be array of object Commented Dec 23, 2019 at 12:17
  • Does this answer your question? Mongoose - Save array of strings Commented Dec 23, 2019 at 12:22
  • that is the same thing i did in the code, didnt still work Commented Dec 23, 2019 at 12:50
  • What's the output of console.log(req.body.Answers) and console.log(typeof req.body.Answers) ? Commented Dec 23, 2019 at 13:05
  • 1
    I don't know, maybe look at the place where you created the request Commented Dec 23, 2019 at 14:05

0

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.