Nicole
Nicole

Reputation: 1416

mongoose .save() hangs with 3 level of subdocuments when called from node.js

I am currently working with mongoose and node.js.

I am using these schemas (shortened for this question):

var gameSchema = new Schema({
    gameId:   { type: Number },
    rounds: [Round.schema]
});

var roundSchema = new Schema({
  roundId : { type: Number },
  lines: [Line.schema] 
}, { _id : false });

var lineSchema = new Schema({
  lineId: { type: Number }
  plays:   [ Play.schema ]
}, { _id : false });

var playSchema = new Schema({
  category:   { type: String },
  score: { type: Number }
}, { _id : false });

I first get the game with a round in the rounds array through a findOne(), and then alter some attributes and push a line with multiple plays to the round. When I try to do a

game.save(function(err) {
            console.log("it's doing the save");
            if(!err) {
              console.log('ok');
            } else {
              console.log('ERROR: ' + err);
            }
          });

the server just hangs and does nothing, none of the console messages prints out. Looking at the mongoose log, I couldn't find any error message or some log that helped me.

The weird thing is that if I replace the plays array with an empty one [], the save works as expected.

Here's an example of the game document I am trying to save:

{ __v: 1,
  _id: 53d427c43ff7f8bc1b9aa3b6,
  gameId: 1,
  rounds: [ 
   { roundId: 1,
     lines: [
       { lineId: 1,
         plays: [ 
          { score: 10,
            category: 'COLORES'
          },
          { score: 10,
            category: 'ANIMALES'
          } 
         ] 
       }
     ] 
   } 
 ] 
}

Any thoughts?

thanks!

Upvotes: 0

Views: 686

Answers (1)

Nicole
Nicole

Reputation: 1416

After days and days of bumping my head into the wall, I figured what my issue was.

My "PlaySchema" had a method definition, that method was called validate.

PlaySchema.method.validate

That method, was the one that was preventing my Play from being saved into the database. Once I changed that method name to something else than "validate", it worked just fine. It seems that mongo treats "validate" as a reserved word.

Upvotes: 2

Related Questions