Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
Correct way to pass javascript variables

I'm having trouble understanding how javascript passes variables. I have the following code running 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
    });

which is calling the following 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);
}

my output is

defenderID johndoe
challengeID somechallenge
attempting to start a game
session {
  session: '620c29582ac275bd67cd3cca',
  defender: 'johndoe',
  challenger: 'somechallenge'
}
defender undefined
challenge undefined

i'm not exactly sure where im going wrong here, since i was referencing another bit of code that works fine doing this

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are passing down an object as a single argument to the Game.start method at the time of calling the function but in your function declaration you are expecting 3 arguments i.e session, defender and challenger. Since session is the first argument in your function declaration, you are getting the object in console.log for session.

You can fix it by passing all the arguments individually when calling the method like this-

  const {
    sessionID,
    defenderID,
    challengerID
  } = req.body;

 console.log('sessionID', sessionID);
 console.log('defenderID', defenderID);
 console.log('challengeID', challengerID);
 
 const newGame = await Game.start(sessionID, defenderID, challengerID);
 

Another way to fix it would be to change the function declaration to accept a single argument which is an object and you can destructure the properties like this

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();
}

And then you can call the method as you are already doing it

const newGame = await Game.start({
  session: sessionID,
  defender: defenderID,
  challenger: challengerID
});

Also, you don't need to use .then after calling .save() as you are already using async await pattern and calling await newGame.save() will return the saved game document.

return await newGame.save();
about 4 years ago · Juan Pablo Isaza Denunciar

0

Whole object is passed to first argument (named session):

{
      session: sessionID,
      defender: defenderID,
      challenger: challengerID
}

You can change call to:

const newGame = await Game.start(sessionID, defenderID, challengerID);

or method definition to:

async (obj) => {
...
const newGame = new Game({
    defender: obj.defender,
    challenger: challenger,
    startTime: date
  });
...
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda