Home > Blockchain >  Correct way to pass javascript variables
Correct way to pass javascript variables

Time:02-16

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

CodePudding user response:

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

CodePudding user response:

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
  });
...
  • Related