node.js - NodeJs looping through object variables behaving strange -
I am running mysql from within a loop, for some reason my variable is being overwritten.
Under my code:
setInterval (function () {for s_bots in var} {if (_bots.hasOwnProperty (s)) {var obj = _bots [ S]; for (var in Obj) {console.log ('-----' _bots [s] [Sahara]. Channel + '-----'); channel = _bots [s] [Sahara] Channel, CC = S; dd = prop; var sql = 'SELECT * FROM `usrs' Where CID =' + connections. Escape (_bots [s] [prop] channel) + 'border1'; connection. Query (SQL, function, result) {if (results.length! = 0) {console.log ('NAME --- & gt; '+ cc;) console.log (' MSG_TO --- & gt; '+ dd); console.log (' id --- & gt; 'channel);} Other {// ....}}}}}}}}, 15000) The problem is that NAME - & gt; MSG_TO - & gt; Variable (CC, DD, and Channel) Always keep the last value from the object. For some reason they are overlapping .. Strangely I do not have permission to use in my code for s and prop inside mysql selection function. I have a >
The expected behavior is that each one of your for..in loop defines a closing, and all closures have the same function scope in their scope chains Refer to. Whereas cc , dd , and channels are all variables in this area, of course you get the same output. If you want different outputs, to force these variables before their values change. Try:
connection.query (SQL, function (CC, DD, channel, fault, result) {if (results.length! = 0) {console.log ('NAME - - & gt; '+ cc;); console.log (' MSG_TO --- & gt; '+ dd); console.log (' id --- & gt; 'channel);} Other {//. ..}} .bund (tap, cc, dd, channel)); // Force values here
Comments
Post a Comment