Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!