node.js - Strange closure issue in javascript -


how come in case getting ['undefined1','undefined2'] value array? pushed in same scope.

router.post('/add', function(req, res) {     var imagearr = [];     for(var = 1; <= 4; i++) {         if (req.body["photo" + i]) {             imagearr.push(req.body.photo + '' + i);         }     }      console.log(imagearr) // working fine here, returning correct values ['something','something']      if (req.body.is_update) {         console.log(imagearr) // working fine here too, returning correct values     } else {         console.log(imagearr) // not working fine here, returning undefined1, undefined2..      } } 

is because of async, possibly?

i thought turn comment formal answer question won't hang unanswered.

you need replace part of script imagearr.push(req.body.photo+''+i); imagearr.push(req.body["photo"+i]);. examples of console outputs both cases can seen in fiddle.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -