5

I am trying to insert a round in a existing game, which gives me the following error:

Game validation failed: rounds.1.questions: Cast to Array failed for value "[ 5ac5cfb41fca8a22f519cb22 ]" at path "questions"

Schema's:

const roundSchema = Schema({
  roundNumber: {
    type: Number,
    required: true,
  },
  categories: {
    type: [String],
    required: true
  },
  questions: {
    type: [Schema.Types.ObjectID],
    ref: 'Question',
    required: true,
  }
});

const gameSchema = Schema({
  code: {
    type: String,
    required: true,
  },
  teams: {
    type: [Schema.Types.ObjectID],
    required: false,
  },
  rounds: [roundSchema]
});

const questionSchema = Schema({
  question: {
    type: String,
    required: true,
  },
  answer: {
    type: String,
    required: true,
  },
  category: {
    type: String,
    required: true,
  }
});

insert function:

function createRoundForGame(game, round) {
  round.questions = round.questions.map((question) => {
    return mongoose.Types.ObjectId(question);
  });

  console.log(round.questions);
  game.rounds.push(round);

  return game.save()
}

Parameter game is:

{ 
   teams: [],
   rounds: 
    [ { categories: [Array],
        questions: [],
        _id: 5ac7507c5491ed422de3ce68,
        roundNumber: 1 } ],
   _id: 5ac74cccc65aac3e0c4b6cde,
   code: '537epG',
   __v: 1 
}

Parameter round is:

{ 
   roundNumber: 1,
   questions: [ '5ac5cfb41fca8a22f519cb22' ],
   categories: [ 'Art and Literature', 'Music', 'Science and Nature' ] 
}

console.log(round.questions) result :

[ 5ac5cfb41fca8a22f519cb22 ]

mongoose : 5.0.12,

I have no idea what i am doing wrong here. And would appreciate some help here.

  • Because you are trying to add string instead of array !!!! Try ``console.log(typeOf round.questions)`` – Devang Naghera Apr 06 '18 at 11:05
  • Thank you for the fast response, `console.log(typeof round.questions)` returns for me object. – DanieldeJong93 Apr 06 '18 at 11:09
  • `console.log(Array.isArray(round.questions))` returns true – DanieldeJong93 Apr 06 '18 at 11:15
  • I think because your questions array field is required, so it must have at least 1 element but your parameter for game has empty questions. I think that causes the problem. – dnp1204 Apr 06 '18 at 12:28
  • @dnp1204 Thanks for the response, I have indeed tried to remove the required constraint, this does not change anything in the result. Also for clarification the parameter game is an already existing game which has been added successfully in a earlier stage. – DanieldeJong93 Apr 06 '18 at 12:34
  • can you please add your question schema? I had similar similar error before when I used a reserved keyword in mongoose – dnp1204 Apr 06 '18 at 12:40
  • @dnp1204 added question schema – DanieldeJong93 Apr 06 '18 at 12:51

1 Answers1

2

Try this..

const roundSchema = Schema({
  roundNumber: {
    type: Number,
    required: true,
  },
  categories: {
    type: [String],
    required: true
  },
  questions: [{type: Schema.Types.ObjectId, ref: 'Question', required: true}]
});
Igal Klebanov
  • 348
  • 2
  • 15
  • Thank you! Totaly fixed it. Still quite wondering why categories does work but questions didn't, could you maybe explain? – DanieldeJong93 Apr 07 '18 at 12:23
  • my bet is, it has something to do with 'ref' not being in an object with an immediate objectID type in the schema. every 'ref' example has {type: ObjectId, ref: 'model'} ... – Igal Klebanov Apr 07 '18 at 13:40