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

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -