title: Why can't Node see an element in an array when it is chosen by variable?
body: <p>In a script I am writing, I am iterating over some messages in a mailbox and attempting to delete ones that have been read. The issue I have been getting though, is that as i iterate over the array, Node says that it cannot find the property id at the given element(which is necessary to delete or view anything). This would be fine if that property didn't exist, but when using util.inspect and specifying 1 instead of the iterator variable, it can find and print that field. The code as it exists is shown below:</p>
  
  <pre><code>             for(var b = 1; b &lt; length; b++){
                  setTimeout(function(){
                    console.log(util.inspect(messageList, false, null));
                    console.log(util.inspect(messageList[1], false, null));
                    console.log(util.inspect(messageList[1].id, false, null));
                    console.log(util.inspect(messageList[b].id, false, null));
                    var delID = messageList[b].id;
                    //console.log(util.inspect(messageList, false, null));
                    if(delID){
                      gmail.users.messages.trash({
                        auth:auth,
                        userId: 'me',
                        id: delID,
                      }, function(err2, delObject){
                        if(err2){
                          console.log("The message with ID: "+ delID + " could not be deleted");
                          console.log(err2);
                          return;
                        }
                        else{
                          console.log(util.inspect(delObject, false, null));
                          console.log("Message was deleted with id: " + delID);
                        }
                      })
                    }
                  }, 2000);
                }
  </code></pre>
  
  <p>The lines above where messageList has an actual number instead of a variable shows '157550983b6c1cb' (the apostrophes print out as well). On the line where I switch it with b, it throws: </p>
  
  <pre><code>    TypeError: Cannot read property of undefined
         at Timeout.oneTimeout(.......etc)
         at tryOnTimeout(timers.js:232:11)
         at Time.listonTimeout(timers.js:202:5)
  </code></pre>
  
  <p>Does this mean that it is impossible to use the variable b inside the Timeout function? If so, how do I get around needing to iterate over the entire messageList with a break in between deletions?</p>