Http response inside function(err,data)

Hie
I am doing some functionality on callback response with “data” with some parameters
it works fine but when I store value in any variable of response"data" for one time value gets changed according to response but later on value gets reset.

Here is little bit idea of mine code

var userloggedin;
http.post(("http//anything",{ "email":this.email.script.input.element.value }, function (err, data))
{
if(data=="this")
{
userloggedin=true;
console.log(""+userloggedin);     // here I am getting userloggedi n correct value true but only 
                                                      for one time 
if(userloggedin)
{
// calling some function here like disabling login Ui screen but it s not working because userlogged in become false
}
});

login.prototype.update(dt)
{
console.log(""+userloggedin);     // here I am getting userlogged in false value;
};

Hey, can you share the complete script code for better understanding?
Seems like it’s a variable scope issue. Is userloggedin a local variable or a global variable?

hello ,
userloggedin is a global variable .

It’s difficult to single out errors without looking at the script. Does the global variable gets updated from anywhere?

There is no more reference of isalreadyloggedin bool
here is my code please check,

DatabaseManager.attributes.add('loginButton',
{
type:'entity'

});

var isalreadyloggedin;
DatabaseManager.prototype.initialize = function() {
    this.isalreadyloggedin=false;
     

this.loginButton.button.on('click', function(event) 
{
    this.UserLogin();

 
  
},this);
};
DatabaseManager.prototype.UserLogin=function()
{
   pc.http.post("http//anything//login.php",{ "Password":this.Password.script.input.element.value }, function (err, data) {
       let res=String(data);   // data is that which I am printing in my php script 
      
        if(res=="")
       {
          
     
       this.isalreadyloggedin=true;
       console.log("userloggedin"+this.isalreadyloggedin);    // here I am getting bool true for only one time if password matches with database.
       
       }});
       
};
DatabaseManager.prototype.update = function(dt) {
  
 if(this.isalreadyloggedin==true)
{
// just want to do some functionality  here but I am always getting isalreadlyloggedin bool value false in console

}
console.log(""+this.isalreadyloggedin);
};

Yeah, so it’s a scope issue. You haven’t bind “this” to the http request function and that’s why it’s considering this.isalreadyloggedin a different variable at line 29.

Try this updated function with “this” binded and it should work.

DatabaseManager.prototype.UserLogin=function()
{
   pc.http.post("http//anything//login.php",{ "Password":this.Password.script.input.element.value }, function (err, data) {
       let res=String(data);   // data is that which I am printing in my php script 
      
        if(res=="")
       {
          
     
       this.isalreadyloggedin=true;
       console.log("userloggedin"+this.isalreadyloggedin);    // here I am getting bool true for only one time if password matches with database.
       
       }}.bind(this));
       
};
2 Likes

Yes,Now its working.
Thanks.