I’m trying to create an object according to my model in mongoose using a controller, but I’m running into a problem that the last array nesting with prize patterns is not being written. Theoretically it is, but it is empty. I do not understand what the problem is, since I cannot add the necessary value to it even after creating the object through the constructor and after that doing something like: newGame.prizes = prizes;
With this behavior, all other properties within the prize property appear in the object, but the pattern array is empty.
Here is what my model looks like:
const GameSchema = mongoose.Schema({
type: String,
roundId: String,
userId: String,
calloutNumbersCount: Number,
tickets: [
{
id: String,
columns: [[Number]],
},
],
prizes: [
{
id: String,
name: String,
nameLocaleKey: String,
winAmount: Number,
patterns: [
{
type: String,
count: Number,
},
],
},
],
ticketPrice: Number,
boughtTicketIds: [String],
ended: Boolean,
});
Controller:
function createGame(gameType, userId) {
const currentGameConfig = config[gameType];
console.log("Controller");
currentGameConfig.prizes.map((el) => console.log(el.patterns));
const roundId = nanoid(LENGTH_OF_ROUND_ID);
const game = new Game({
type: currentGameConfig.type,
roundId,
userId,
calloutNumbersCount: currentGameConfig.callout.numbersCount,
tickets: [
{
id: 123, // temp
columns: [[0]], // temp
},
],
prizes: currentGameConfig.prizes,
ticketPrice: currentGameConfig.tickets.price,
boughtTicketIds: ["1", "2"], // temp
ended: false,
});
return game;
}
Place of creation and modification of the object in the route:
if (!game) {
const newGame = gameController.createGame(type, userId);
// newGame.prizes = currentGameConfig.prizes;
Game.addGame(newGame, (err, game) => {
if (err) {
res.json({ success: false, message: err.message });
throw err;
}
return res.json({
roundId: newGame.roundId,
});
});
}
Expected result: