I come from a PHP background and have started using node.js. Most things I am doing okay on but there are some thing thats I am having a hard time wrapping my head around when it comes to sync vs async and scope.
Here is a pretty simple example:
app.get('/register/:invite_id?' function(req, res) {
var agent = superagent.agent();
var form = {};
agent.post('127.0.0.1/invite/' + req.params.invite_id + '/details')
.end(function(invite_error, invite_details) {
form.email = invite_details.body.user.email;
//I can console.log form.email here
});
// I cannot console.log form.email here.. i get undefined.
// I need to access form.email here, so I can pre-populate my form field below...
// Sometimes however that agent.post may take 2-3 seconds
res.render('user/register', {
form: form
});
});