0

I sent this request which is an array of objects from reactjs app to nodejs(sequelize,mysql)backend

{  
   "Qusetion":"new question",
   "Qusetiontype":"AddedAnswers",
   "Qusetionanswers":[  
      {  
         "QuestionAnswer":"a1"
      },
      {  
         "QuestionAnswer":"a2"
      }
   ],
    "Surveyid":"9fe96b40-7704-11e9-b46f-3f2c20a71682"
}

the nodejs script

 QuestionAnswer.create(req.body.Qusetionanswers.map(Answer=>{
        Qusetionanswer=Answer.QuestionAnswer,
        QuestionId=qid
    })       
    ).then(res=>{console.log(res)}).catch(err=>{console.log(err)})

My Problem: I got below error msg in postman when trying to send new question I searched and found that create not accept an array of objects and nothing else.so any help thanks in advance

{
    "message": "this.build(...).save is not a function"
}
1
  • half of my problem solved by using bulkCreate to insert an array of objects but the other half is I want to insert id with this bulk which is not attached to that array Commented May 26, 2019 at 8:03

1 Answer 1

2

I would try it likes this. Creating one after one. Or are you trying to archive anything else?

  req.body.Questionanswers.map(answer => {
    QuestionAnswer.create({
        Qusetionanswer: answer.QuestionAnswer,
        QuestionId: qid
      }).then(res => {console.log(res);}).catch(err => {console.log(err);}
    );
  });
Sign up to request clarification or add additional context in comments.

3 Comments

Cannot read property 'map' of undefined...yes i put inside then...i used bulkcreate and its worked but couldnt insert QuestionId with this array @Montezuma
When Qusetionanswers is undefined something else is wrong. Sounds like a bad request.
if you have an array of objects it's better to use blukCreate but for this problem, because I want to insert another value with that array I used @Montezuma solution.

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.