So I am trying to pass json data to my req.body
The data looks like this:
answers = ["A","B"]; //This is an array that will be added to the JSON Object
var Student_Answers = { //This is the JSON object
Answers: answers,
matricNumber: matric
};
This is the post request:
$.ajax({
type: "POST",
url: `/exam/${matric2}`,
dataType: "json",
data: Student_Answers,
success: function(data) {
console.log("data received: ", data);
}
});
So the data got passed to the req.body successfully but my problem is how the data looks like in the req.body output, when I logged the req.body it looks like this:
{
'Answers[]': [ 'A', 'B' ],
matricNumber: '88/2386'
}
But what I am expecting
{
Answers: [ 'A', 'B' ],
matricNumber: '88/2386'
}
Can someone explain to me why the answers array is viewed that way in the req.body ('Answers[]': [ 'A', 'B' ]).it is messing up my saving the data to mongodb.