res.redirect happens before your roll_new_id function call has a chance to return the new id value. To get around this you have to call roll_new_id first, and pass in a callback function that it will call once it is done creating the new id. Once the new id value has been created it will execute the callback function passing the new id value as an argument. Inside the callback function that is passed to roll_new_id you'll do res.redirect using the new id value that was passed from roll_new_id when it executed the callback function. Does this make sense?