Reputation: 93
I'm trying to save the data in the array of fields uisng node js , but here database is showing with the empty fileds , what i'm doing wrong here. please help me thanks in advance
schema Design:-
module.exports = mongoose => {
const Question = mongoose.model(
"question",
mongoose.Schema(
{
roleType : { type:String },
questions : [{
question : { type:String },
options : [{ type:String }]
}]
})
)
return Question;
};
code:-
const { roleType, question, options } = req.body
const questionList = new Question ({
roleType: roleType,
questions: [{
question:question,
options: options
}],
});
questionList.save(questionList)
postman i'm passing data like this:-
database is storing like this:-
Upvotes: 1
Views: 75
Reputation: 20626
You are destructuring your object in the wrong manner.
const { roleType, question, options }
. questions and options inside the braces expect the respective properties question
and options
in the body object. Since they do not exist, question
, options
are undefined.
Simply pass in questions:
const { roleType, questions} = req.body
const questionList = new Question ({
'roleType': roleType,
'questions': questions,
});
questionList.save(questionList);
Upvotes: 2