Title

Wednesday, 4 February 2015

Get 3rd party API with Express JS and Jade


I am having some trouble getting information for Instagram's API and the sending it to Jade to be rendered on the front end.

app.route("/api")  .get(function(req, res){   var url = "https://api.instagram.com/v1/users/1234/media/recent/?client_id=XXXX";     request(url, function(err, res, data){   console.log(JSON.parse(data));   // does this go here    res.render('show', {media : data});   });   // or here?   res.render('show', {media : data});  }); 

I am trying to collect 10 images from this API path and send them to Jade. I am getting a parsed response in my terminal of several entries. I am having a hard time figuring out how to send multiple responses to a Jade file, then having the Jade file loop through them.

I do know that the user ID and client ID in the url variable are not correct. If you have any alternate method to using request(), I am open to that as well. Please let me know if you have any questions.

Thanks in advance,

Answer

So I am answering my own question on this one. The rendering has to be inside of the request() function. The issue was in my callbacks. The issue was that I had "res" as a response for my .get and my request() callback. When I changed the "res" in the request() function to "response" I was not getting any issues anymore. Jade file is below as well

app.route("/api2")  .get(function(req, res){     var url = "https://api.instagram.com/v1/users/1234/media/recent/?client_id=XXXX";     request(url, function(err, response, body){   var dataGram = JSON.parse(body);   res.render('show', dataGram);   });  });

Jade File:

each thing in data   h1 id : #{thing.id}   img(src="#{thing.images.thumbnail.url}")   a(href="#{thing.link}" target="_blank") link   h3 filter : #{thing.filter}

No comments:

Post a Comment