0

I'm I'm trying to create sub arrays with a mongoose and I have a dificulty to insert the subdocuments. My schema is like this:

     var SurveySchema = mongoose.Schema({
      question_id: { type: String, unique: true },
      question_text: { type: String, required: true }, 
      //answer's object. will contain 4 answers
      answers: { type:
        [{
          answer_id: { type: String, unique: true },
          answer_text: { type: String, required: true }, 
          next_question: { type: String, required: true },
          platforms: {
            type: [{
              platform_id: { type: String, required: true },
              platform_name: { type: String, required: true },
              platform_weight: { type: Number, required: true },
            }]
          }
        }]
      }
    });
var SurveySchemaExport = module.exports = mongoose.model('Survey', SurveySchema);

And the data that I want to insert into the db looks like this:

{
    "question_id": "1",
    "question_text": "Freddy",
        "answers": [{
            "answer_id": "1",
            "answer_text": "test1",
            "next_question": "ans02",
            "platforms": [{
                "platform_id": "1",
                "platform_name": "Facebook",
                "platform_weight": "0.5"
            }]
        },
        {
            "answer_id": "2",
            "answer_text": "test2",
            "platforms": [{
                "platform_id": "1",
                "platform_name": "Facebook",
                "platform_weight": "0.2"
            }]
        },        {
            "answer_id": "3",
            "answer_text": "test3",
            "platforms": [{
                "platform_id": "1",
                "platform_name": "Facebook",
                "platform_weight": "0.3"
            }]
        },        {
            "answer_id": "4",
            "answer_text": "test4",
            "platforms": [{
                "platform_id": "1",
                "platform_name": "Facebook",
                "platform_weight": "0.7"
            }]
        }]

}

My insertion function is like this:

  var input = req.body;
  var newAlgoData = new SurveySchema({
    question_id: input.question_id,
    question_text: input.question_text,
    answers: [input.answers],
    next_question: input.next_question,
    platforms: [input.answers.platforms]
  })
  console.log(newAlgoData);
  var isCreated = newAlgoData.save((function (err) {
    if (err){
      return false; 
    } else{
      return isCreated; 
    }
  }))

The responce I get is:

{ _id: 5b79c144f0a8071048aa8f39,
  question_id: '1',
  question_text: 'Freddy',
  answers: [ { _id: 5b79c144f0a8071048aa8f3a, platforms: [] } ] }
(node:4168) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: newAlgoData.save(...) is not a function

I know how to fix the "not a function" error' ,but my problem is with the insertion of the data into the db, specifically the data in the arrays.

Thank you very much for your help.

gusya59
  • 45
  • 7
  • First thing: you are using `QuestionSchema` and `SurveySchema` - which one is correct in this situation? Remember that you need to create a mongoose `model` to be able to save the data - for reference [mongoose model](https://mongoosejs.com/docs/models.html). Second: `var isCreated = await newAlgoData.save()(function (err) { if (err){ return false; } else{ return isCreated; }` if you are using async/await, you must not pass the callback function - just use `isCreated`. Other than that, which node version are you using? – Patryk Wojtasik Aug 19 '18 at 19:44
  • Thank you. I edited my code accordingly to your reply. And my node version is: 8.9.4. – gusya59 Aug 20 '18 at 08:53
  • In this case, it looks like you are putting an array into array: `answers: input.answers,` instead of `answers: [input.answers],` should work (`input.answers` is already an array). Also you need to extract `platforms` from array `input.answers` if you want to put them into `platforms` ([solution here](https://stackoverflow.com/a/46694321/1777334)). – Patryk Wojtasik Aug 20 '18 at 20:14
  • Thanks. answers: input.answers did the trick and resolved the problem. – gusya59 Aug 23 '18 at 14:32

0 Answers0