0

I am new to JavaScript. In the code below, when I log in, I try to get the user info from database and store in user object. However, when I check the user in the commandManager scope, it turns null. How can I make user not null?

Here is the code:

function LogInCommand(username, password) {
  return new Command(function (user) {
    api
      .logIn(username, password)
      .then(function (data) {
        user = data;
        console.log(user);
      })
      .catch(function (error) {
        console.log(error);
      });
  });
}

function createCommandManager() {
  var user = null;

  return {
    execute: function (command, ...args) {
      command.execute(user, ...args);
    },
    user: user
  };
}

var commandManager = createCommandManager();

commandManager.execute(new LogInCommand("[email protected]", "123456"));
setTimeout(function() {
  console.log(commandManager.user);
}, 10000);

Here is the result:

{age: 21, connection: null, email: "[email protected]", name: "andy", pendingConnection: null, …}
null

Thanks all of you for your helps!

1
  • The code seems incomplete. commandManager is undefiend at the line commandManager.execute(new LogInCommand("[email protected]", "123456")); Commented Apr 10, 2020 at 15:37

2 Answers 2

2

Since you change your user variable directly. It will break reference and it will not work. If you want to do it with your way. You can change your code like the below.

function LogInCommand(username, password) {
  return new Command(function (initialData) {
    api
      .logIn(username, password)
      .then(function (data) {
        initialData.user = data;
        console.log(initialData.user);
      })
      .catch(function (error) {
        console.log(error);
      });
  });
}

function createCommandManager() {
  var initialData = {
     user: null,
  };

  return {
    execute: function (command, ...args) {
      command.execute(initialData, ...args);
    },
    data: initialData
  };
}

commandManager.execute(new LogInCommand("[email protected]", "123456"));
setTimeout(function() {
  console.log(commandManager.data.user);
}, 10000);
Sign up to request clarification or add additional context in comments.

5 Comments

console.log(user); will cause an error because there is no user variable in scope.
@Titus thanks for the correction. I have changed the issue.
@HalilİbrahimÖzdoğan Can you explain to me why it breaks the reference? I still don't understand it. Thanks!
In javascript, When you use pass by value technique. Changing the argument inside the function doesn’t affect the variable passed from outside the function. But when you use pass by reference technique. Changing the argument inside the function affects the variable passed from outside the function. In Javascript, objects and arrays follow pass by reference. Actually you were trying pass by reference. But the issue is if you assign your arguments directly, it will break the reference and it will not affect the variable passed from outside the function. That's why we wrapped the variable.
@HalilİbrahimÖzdoğan Oh i got it. Thank you so much!
-1

One the things that are ovious in the code are that there not closed in the correct wy and that there are command that are missing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.