For example, I want to retrieve some data from an API (a User) so I can retrieve more data (a Team associated with that User). Something like:
var fetch = require('node-fetch');
app.get('/users/:username', function (req, res) {
var username = req.params.username;
var user = new Object();
fetch('https://api.github.com/users/' + username)
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
user.handle = json.login;
}).then(fetch('https://api.github.com/users/' + username + '/repos')
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
//user.repos = repos
var payload = new Object();
payload.user = user;
console.log(payload);
res.send(payload);
})
);
});
I'm pretty new to Node, and am having trouble figuring out how to do this correctly. The first fetch call works fine, but the nested one, not so much. There's no error message to point me in the right direction.