Function Return Java Script

Asked

Viewed 177 times

0

I did a lot of research on callbacks and asynchronous in javascript.

Function Helloworld.prototype.getCont searches the ID of an account and returns an EX number: 1; Function Helloworld.prototype.getAllAccounts Must get the result number ex (1) and get all the account data, but it runs the function, shows the result but not saved in the variable; Because he doesn’t save and how can I save him?

Here is where it should get the result, but it just runs the function and does not save the return.

var Member = this.getCont(); /SHOW DATA AGAIN

rewriting, how to get a variable with the saved data?

HelloWorld.prototype.getCont = function(cb) { //GetCont é um objeto Função
 var MyAddress = window.web3.eth.accounts[0];
 document.getElementById("Address").innerHTML =  MyAddress ;
         this.instance.AccountAddress(MyAddress, function (error, result) {      
  document.getElementById("IDAccount").innerHTML =  result ;
 console.log (result); //DATA OK
 return result; 
});  
}


HelloWorld.prototype.getAllAccounts = function(cb) {
       var IDMembro =  this.getCont(); //SHOW DATA AGAIN
 console.log (IDMembro); //VALUE INDEFINED??

          this.instance.AllAccounts(1, function (error, result) {      
 document.getElementById("AllAccounts0").innerHTML =  result[0];
  document.getElementById("AllAccounts1").innerHTML =  result[1];
   document.getElementById("AllAccounts2").innerHTML =  result[2];
    document.getElementById("AllAccounts3").innerHTML =  result[3];
     document.getElementById("AllAccounts4").innerHTML =  result[4];
      document.getElementById("AllAccounts5").innerHTML =  result[5]; 

  });

}

//____________________________________________________________ in the console it returns me this data in array format.

e {s: 1, e: 0, c: Array(1)}c: [1]e: 0s: 1__proto__: Object

but I have another block that it returns the value only with result. this is an account registered in blockchain. ee must return the account ID in case number 1 but always returns undefined in this block. but in another block it comes back normal.

//_____________________________________________________________ Doing so I think it is easier to try to understand because now I have two functions within one. in my database it looks like this: Email Address | Member ID;

I need to search the member ID by the email address it provides to me.

Within the getCont function there is a local variable, two obejects and two functions;

The Member ID is a local variable of the getCont function and takes indefinite. address is a local variable representing the email provided by the user.

Accountaddress is the object with the name given to my structure in the database, the variable address looks for an ID value;

ex: email address search [email protected] and returns the Member ID and sets the Idmember variable that in the example should be 1; 1st member registration. and Idmember is now set; So far everything works.

Now comes the problem: when I take the Idmember that is in the local scope and do console.log outside the Accountaddress object, it automatically comes back undefined.

I understood that it has the sicronia and callback... blz, how can I callback the Accountaddress object since, it has a function and a return? follows the code below:

Thank you very much

Helloworld.prototype.getCont = Function(cb) ː var Idmember; var endereco = window.Web3.eth.Accounts[0] ;

Document.getElementById("Address"). innerHTML = address; this.instance.Accountaddress(address, Function(error, result) {
var Idmember = result; Document.getElementById("Idaccount"). innerHTML = Idmember ;
console.log(Idmember);

}) console.log(Idmember); }

  • Marcio this is the OS site in Portuguese, translate your question

  • translated Ricardo, I thought in English the answer would come faster :) Thank you very much for the tip

1 answer

1

The problem is the scope of the result, it is inside a function that is inside another function that is being called by another function, follows an example of code in which getCont happens to have a result return.

HelloWorld.prototype.getCont = function(cb) { //GetCont é um objeto Função
     var MyAddress = window.web3.eth.accounts[0],
         resultado; // escopo de HelloWorld.prototype.getCont

     document.getElementById("Address").innerHTML =  MyAddress ;
     this.instance.AccountAddress(MyAddress, function (error, result) {      
         document.getElementById("IDAccount").innerHTML =  result ;
         console.log (result); //DATA OK
         resultado = result; // para ter acesso no escopo externo
         return result; // escopo de function (error, result) {
     });  

     return resultado; // escopo de HelloWorld.prototype.getCont
};

With the example after edits, using a callback function:

var funcaoCallback = function(idMember) {
    // faça algo com idMember
};

HelloWorld.prototype.getCont = function(cb) { 
    var IDMember; 
    var endereco = window.web3.eth.accounts[0] ;

    document.getElementById("Address").innerHTML = endereco; 
    this.instance.AccountAddress(endereco, function (error, result) {
        var IDMember = result; 
        document.getElementById("IDAccount").innerHTML = IDMember ;
        console.log(IDMember);
        funcaoCallback(IDMember); // usando callback
   }); 

   console.log(IDMember); 
}
  • 1

    If AccountAddress is asynchronous this code is wrong... because the function will give return before resultado receive the value of result.

  • Yes, that’s why I didn’t advise you to directly re-turn this.instance.Accountaddress, because the code of this method was not displayed, but the answer is valid, regardless of what happens in Accountaddress, it has to change the scope.

  • It didn’t work :( I had been trying to create a local variable outside the Accountaddress scope. but still it doesn’t come back. every time I put a variable between " }); ", it comes back undefined. I think he’s reading first all the variables outside the function and then going into the scope of the function. so be coming back forever indefinite. Any solution if that’s it Thank you very much.

  • You can post the contents of the Accountaddress method?

  • I further simplified the code and explained the situation. Thank you guys

Browser other questions tagged

You are not signed in. Login or sign up in order to post.