My question is how to make sure my variables get loaded before the page gets rendered ? I use node.js with express and everyauth.
This is the code:
app.get('/', function(req, res) {
app.locals.rep = global.repos;
res.render('index');
});
Currently, the page gets loaded, but the paragraph containing the variable is empty and I get an error: "Cannot read property '...' of undefined [...]". As I refresh the page, the content flows in.
index.jade
extends layout
block content
h1= title
- if (!everyauth.loggedIn)
a(href="/auth/github") Login with github
- else
h3
a(href='/logout') Logout
h3 GitHub User Data
- each r in rep
p= r.name
p= r.description
This is where I set the repos variable:
var repos;
global.repos = [];everyauth.github
.appId(config.gh_clientId)
.appSecret(config.gh_secret)
.findOrCreateUser( function (sess, accessToken, accessTokenExtra, ghUser) {if (typeof usersByGhId[ghUser.id] === 'undefined') { usersByGhId[ghUser.id] = addUser('github', ghUser); var options = { host: "api.github.com", path: "/users/cmarius02/repos", method: "GET", headers: { "User-Agent": "github-connect" } }; var request= https.request(options, function(response){ var body=''; response.on("data", function(chunk){ body+=chunk.toString("utf8"); }); response.on("end", function(){ var json=JSON.parse(body); // console.log(json); global.repos = []; for (var k in json) if ({}.hasOwnProperty.call(json, k)) { global.repos.push({ name: json[k].name, description: json[k].description }); } }); }); request.end(); return usersByGhId[ghUser.id]; } else { return usersByGhId[ghUser.id]; } })
.redirectPath('/');
It's my first day of node.js so please be pacient. Thank you in advance.