1

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:-

enter image description here

database is storing like this:-

enter image description here

1 Answer 1

2

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);
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.