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
– Ricardo Pontual
translated Ricardo, I thought in English the answer would come faster :) Thank you very much for the tip
– Marcio Livramento