节点无法读取属性' forEach'未定义的

时间:2015-11-30 10:39:03

标签: javascript node.js express

我收到此错误,我无法找到解决方案我的代码是正确的我认为这是视频课我可以通过instagra api从instaram显示流行的照片但我得到这当我尝试执行节点运行正常我可以理解我的错误

TypeError:\Desktop\instagramApp\views\pages\index.ejs:13
   11| <main>
   12| <div class="row">
>> 13| <% grams.forEach(function(gram)  { %>
   14| <div class="instagram-pic col-sm-3">
   15| <a href="<%= gram.link %>"target="_blank">
   16|  <img src="<%= gram.images.standard_resolution.url %>" class="img-responsive">

Cannot read property 'forEach' of undefined
   at eval (eval at <anonymous> (C:\Users\Kel3vra\Desktop\instagramApp\node_modules\ejs\lib\ejs.js:464:12), <anonymous>:23:13)
   at returnedFn (C:\Users\Kel3vra\Desktop\instagramApp\node_modules\ejs\lib\ejs.js:493:17)
   at View.exports.renderFile [as engine] (C:\Users\Kel3vra\Desktop\instagramApp\node_modules\ejs\lib\ejs.js:350:31)
   at View.render (C:\Users\Kel3vra\Desktop\instagramApp\node_modules\express\lib\view.js:126:8)
   at tryRender (C:\Users\Kel3vra\Desktop\instagramApp\node_modules\express\lib\application.js:639:10)
   at EventEmitter.render (C:\Users\Kel3vra\Desktop\instagramApp\node_modules\express\lib\application.js:591:3)
   at ServerResponse.render (C:\Users\Kel3vra\Desktop\instagramApp\node_modules\express\lib\response.js:961:7)
   at C:\Users\Kel3vra\Desktop\instagramApp\server.js:24:7
   at handle_error (C:\Users\Kel3vra\Desktop\instagramApp\node_modules\instagram-node\lib\instagram.js:255:14)
   at C:\Users\Kel3vra\Desktop\instagramApp\node_modules\instagram-node\lib\instagram.js:904:16 

这是index.ejs的代码

     <!DOCTYPE html>
    <html>
    <head>
        <% include ../partials/head %>
        <title></title>
    </head>
    <body class="container">
        <header>
        <% include ../partials/header %>
        </header>
        <main>
        <div class="row">
        <% grams.forEach(function(gram)  { %>
            <div class="instagram-pic col-sm-3">
            <a href="<%= gram.link %>"target="_blank">
              <img src="<%= gram.images.standard_resolution.url %>" class="img-responsive">
            </a>
            </div>

            <div class="instagram-bar">
                <div class="likes">
                <span class="glyphicon glyphicon-heart"></span>
                <%= gram.likes.count %>
                </div>
            </div>

            <div class="comments">
            <span class="glyphicon glyphicon-comment"></span>
                <%= gram.comments.count %>
            </div>
          <%  }); %>


         </div>
        </main>

        <footer>
        <% include ../partials/footer %>
        </footer>
    </body>

    </html>

和我的server.js

var express = require('express');    
var app = express();
var instagram = require('instagram-node').instagram();

app.use(express.static(__dirname + '/public'));    
app.set('view engine', 'ejs');

instagram.use({
  client_id: /************/,
  client_secret: /*************/
});

app.get('/',function (req, res) {
  instagram.media_popular(function (err, medias, remaining, limit) {
    res.render('pages/index',{ grams: medias });
  });
});

app.listen(8080, function (err) {
  if(err) {
    console.log("error");
  } else {
    console.log("Listening on port 8080")
  }
});

3 个答案:

答案 0 :(得分:0)

看起来像这行中的变量medias

res.render('pages/index',{ grams: medias });

...未定义 - 首次访问时(在模板中)抛出错误。

instagram.media_popular函数可能会返回错误,您可以通过更新路线来检查错误:

app.get('/',function (req, res, next) {
  instagram.media_popular(function (err, medias, remaining, limit) {
    if(err) return next(err);
    res.render('pages/index', { grams: medias });
  });
});

(注意顶部的额外next,这允许您将错误传递给快速错误处理代码)

答案 1 :(得分:0)

我遇到了同样的问题...

问题是使用了来自instagram api的错误client_id

答案 2 :(得分:0)

我遇到了同样的问题而不是客户端ID和客户端秘密我使用了Access令牌,错误消失了。

ig.use({
   access_token: 'Enter your access token here'
});