Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda