I'm struggling to grasp how JavaScript handles variable passing. The code snippet below is being executed in an Express controller:
const {
sessionID,
defenderID,
challengerID
} = req.body;
console.log('sessionID', sessionID);
console.log('defenderID', defenderID);
console.log('challengeID', challengerID);
const newGame = await Game.start({
session: sessionID,
defender: defenderID,
challenger: challengerID
});
This code snippet references the following function definition:
gameSchema.statics.start = async (session, defender, challenger) => {
const date = new Date();
console.log('attempting to start a game');
console.log('session', session);
console.log('defender', defender);
console.log('challenge', challenger);
const newGame = new Game({
defender: defender,
challenger: challenger,
startTime: date
});
newGame.session.push(session);
return await newGame.save().then((game) => game);
}
The current output shows:
defenderID johndoe
challengeID somechallenge
attempting to start a game
session {
session: '620c29582ac275bd67cd3cca',
defender: 'johndoe',
challenger: 'somechallenge'
}
defender undefined
challenge undefined
I am puzzled by this discrepancy since I have another piece of code that performs similar actions without any issues.