I have a Schema called Players and another one called Game. Every Games has an attribute called players which is an array of references to player objects.
Game Schema
let GameSchema = new mongoose.Schema({
players: [{
ref: "Player",
type: mongoose.Schema.Types.ObjectId,
}],
created: {
type: Date,
default: Date.now,
}
}, { usePushEach: true });
PlayerSchema
let PlayerSchema = new mongoose.Schema({
parentGameId: mongoose.Schema.Types.ObjectId,
idInGame: Number,
points: Number,
});
MongoDb Connection
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost/ScoreKeeper", { useMongoClient: true });
Code for initializing Games and Players on the post route. (I am using Express)
app.post('/', function(req, res) {
let players = Number(req.body.players);
Game.create({
players: [],
}, function(error, newGame) {
if (error) {
console.log(error);
} else {
let currentGameID = newGame._id;
for (let i = 1; i <= players; i++) {
Player.create({
parentGameId: currentGameID,
idInGame: i,
points: 0
}, function(error, newPlayer) {
if (error) {
console.log(error);
} else {
newGame.players.push(newPlayer._id);
newGame.save(function(error, newGame) {
if (error) {
console.log(error);
} else {
console.log("New Player added");
}
});
}
});
}
}
});
});
On Terminal on the node console I have the following output, when I create 5 players:
New Player added
New Player added
New Player added
New Player added
New Player added
Now when I go to the mongo console and check out the players collection, I find there are 5 players created as expected. Using "db.players.find()"
{ "_id" : ObjectId("5a6159685d3dabb00d0065b0"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 1, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6159685d3dabb00d0065b1"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 2, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6159685d3dabb00d0065b2"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 3, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6159685d3dabb00d0065b3"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 4, "points" : 0, "__v" : 0 }
{ "_id" : ObjectId("5a6159685d3dabb00d0065b4"), "parentGameId" : ObjectId("5a6159685d3dabb00d0065af"), "idInGame" : 5, "points" : 0, "__v" : 0 }
However when I check the game object in the mongo console, I find this ...
db.games.findOne()
{
"_id" : ObjectId("5a6159685d3dabb00d0065af"), "created" : ISODate("2018-01-19T02:35:20.632Z"), "players" : [ ObjectId("5a6159685d3dabb00d0065b1"), ObjectId("5a6159685d3dabb00d0065b1"), ObjectId("5a6159685d3dabb00d0065b0"), ObjectId("5a6159685d3dabb00d0065b1"), ObjectId("5a6159685d3dabb00d0065b0"), ObjectId("5a6159685d3dabb00d0065b2"), ObjectId("5a6159685d3dabb00d0065b3"), ObjectId("5a6159685d3dabb00d0065b3"), ObjectId("5a6159685d3dabb00d0065b4") ], "__v" : 5
}
There is a mismatch between Games and Players, I feel that the bug is in saving the newGame
after adding newPlayer
. Strangely enough _v
property reads 5 as expected. Could you please point me towards fixing the bug?
Thank-you