0

I am struggling how to create a flag in nodejs. Please refer to small code below to get my idea

var isLogged = "no"

function login(){
    this.isLogged = "yes"
}

function sendText(){
    if(this.isLogged == "yes"){
      console.log("Hello World")
    }
}

In nodeJS, variable seems not to support this. Please advice me how to do it. Thanks

Phong Vu
  • 2,726
  • 6
  • 24
  • 52
  • Do you have an actual question? Did you see some sort of error? What was it? Did something not work as you expected? What happened, and what did you expect to happen? – user94559 Jul 07 '17 at 17:05
  • create a parent object – Rick Jul 07 '17 at 17:05
  • 1
    What is `this` referring to here? (That depends on how these functions are called.) Perhaps `var isLogged = "no"` should be `this.isLogged = "no"` in a constructor, or perhaps `this.isLogged` should just be `isLogged` elsewhere. There's really not enough information here to help you. – user94559 Jul 07 '17 at 17:06
  • you'd need to persist the data in sessions for such flags. if you are using express, use express-session along backed by redis or mongodb to store session data. – sbharti Jul 07 '17 at 17:09
  • Sorry, if I made you guys confused, I am so new with node js, so sometimes I don't really know the issue, just try to explain my idea. – Phong Vu Jul 07 '17 at 17:38

2 Answers2

2

no windows object in node use variable indexing. like this.

var isLogged = 'no'

function login() {
  isLogged = "yes"
}

function sendText() {
  if (isLogged == "yes") {
    console.log("Hello World")
  }
}

console.log(isLogged);
login()
sendText();
console.log(isLogged);
Rick
  • 1,035
  • 10
  • 18
0

Maybe you should just try to go simple, and forget the this scope. Just reassign.

var isLogged = "no";

function login(){
    isLogged = "yes";
}

function sendText(){
    if(isLogged == "yes"){
      console.log("Hello World");
    }
}