如何使用Express JS和Jade访问instagram API ?

时间:2021-07-12 07:25:24

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

我在为Instagram的API获取信息并将其发送给Jade以便在前端渲染方面遇到了一些麻烦。

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.

我正在尝试从这个API路径中收集10张图片并发送给Jade。在我的几个条目的终端中,我得到一个解析响应。我很难弄清楚如何向一个Jade文件发送多个响应,然后让这个Jade文件循环遍历它们。

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.

我知道url变量中的用户ID和客户端ID是不正确的。如果您有使用request()的替代方法,我也愿意这样做。

1 个解决方案

#1


4  

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

我在回答我自己的问题。呈现必须位于request()函数的内部。问题在我的回调中。问题是,我有“res”作为.get和request()回调的响应。当我将request()函数中的“res”更改为“response”时,我再也没有收到任何问题。翡翠档案也在下面

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}

#1


4  

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

我在回答我自己的问题。呈现必须位于request()函数的内部。问题在我的回调中。问题是,我有“res”作为.get和request()回调的响应。当我将request()函数中的“res”更改为“response”时,我再也没有收到任何问题。翡翠档案也在下面

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}