【问题标题】:"Not defined" using multiple callback functions (Node + EJS)“未定义”使用多个回调函数(Node + EJS)
【发布时间】:2015-04-23 14:43:31
【问题描述】:

在我的 app.js 中,我有以下内容:

app.get('/', function(request, response, next) {
    myLocation.find(function(err, locations) {
        if (err) {
            response.send(501, 'There was an error');
        }
        else {
            response.render('index', {
                locations:locations
            });
        }
        next();
    });
        app.get('/', function(request, response) {
        mySupplier.find(function(err, suppliers) {
            if (err) {
                response.send(501, 'There was an error');
            }
            else {
                response.render('index', {
                    suppliers:suppliers

                });
            }
        });
    });
});

然后我尝试在我的 index.ejs 中显示位置和供应商的所有信息。我可以让两者单独工作,但不能同时工作。我假设我对“/”的多个回调函数做错了。

这是我的 index.ejs

<div class="row">
            <div class="span12" style="border: 2px solid black">
                <% locations.forEach(function(location) { %>
                    <div>
                        <p><%= location.siteName %>,
                        <%= location.siteAddress %>,
                        <%= location.sitePhone %>,
                        <%= location.managerName %>,
                        <%= location.managerContact %>,
                        <%= location.managerEmail %></p>
                    </div>
                <% }) %>
            </div>
            <div class="span12" style="border: 2px solid black">
                <% suppliers.forEach(function(supplier) { %>
                    <div>
                        <p><%= supplier.supName %>,
                        <%= supplier.supAddress %>,
                        <%= supplier.supPhone %>,
                        <%= supplier.supAltPhone %>,
                        <%= supplier.supEmail %>,
                        <%= supplier.supContact %></p>
                    </div>
                <% }) %>
            </div>
        </div>  

我已经尝试解决这个问题好几个小时了,但还是想不通。

任何帮助将不胜感激,如果需要,这是我收到的错误

ReferenceError: e:\Coding\wineCellar\views\index.ejs:34
    32|             </div>
    33|             <div class="span12" style="border: 2px solid black">
 >> 34|                 <% suppliers.forEach(function(supplier) { %>
    35|                     <div>
    36|                         <p><%= supplier.supName %>,
    37|                         <%= supplier.supAddress %>,

suppliers is not defined
    at eval (eval at <anonymous> (e:\Coding\wineCellar\node_modules\ejs\lib\ejs.js:299:12), <anonymous>:2:3361)
    at e:\Coding\wineCellar\node_modules\ejs\lib\ejs.js:325:14
    at View.exports.renderFile [as engine] (e:\Coding\wineCellar\node_modules\ejs\lib\ejs.js:195:31)
    at View.render (e:\Coding\wineCellar\node_modules\express\lib\view.js:75:8)
    at Function.app.render (e:\Coding\wineCellar\node_modules\express\lib\application.js:504:10)
    at ServerResponse.res.render (e:\Coding\wineCellar\node_modules\express\lib\response.js:753:7)
    at Promise.<anonymous> (e:\Coding\wineCellar\app.js:64:13)
    at Promise.<anonymous> (e:\Coding\wineCellar\node_modules\mongoose\node_modules\mpromise\lib\promise.js:177:8)
    at Promise.emit (events.js:95:17)
    at Promise.emit (e:\Coding\wineCellar\node_modules\mongoose\node_modules\mpromise\lib\promise.js:84:38)

【问题讨论】:

    标签: node.js mongodb mongoose ejs


    【解决方案1】:
    app.get('/', function(request, response, next) {
        myLocation.find(function(err, locations) {
            if (err) {
                response.send(501, 'There was an error');
            }
            else {
                response.locals.locations = locations;
            }
            next();
        });
    

    你可以试试这个方法。

    【讨论】:

      【解决方案2】:

      您的问题是您没有在这里为您的根路由使用“多个回调函数”。以下是代码的执行方式:

      // 1. Define a route matching all get requests at '/'
      app.get('/', function(request, response, next) {
          // 2. myLocation.find is invoked (async)
          myLocation.find(function(err, locations) {
              if (err) {
                  response.send(501, 'There was an error');
              } else {
                  // 5. Your response is rendered ???
                  response.render('index', {
                      locations: locations
                  });
              }
              // 6. Attempt to invoke the next route match
              next();
          });
          // 3. A new route matcher is defined for '/'
          app.get('/', function(request, response) {
              mySupplier.find(function(err, suppliers) {
                  if (err) {
                      response.send(501, 'There was an error');
                  } else {
                      response.render('index', {
                          suppliers: suppliers
      
                      });
                  }
              });
          });
          // 4. app.get defined in #1 exits
      });
      

      我建议尝试以下方法:

      app.get('/',
          function(request, response) {
              // immediately execute location search
              myLocation.find(function(err, locations) {
                  // fail if error in location search
                  if (err) {
                      return response.send(501, 'There was an error');
                  }
      
                  // otherwise, do a supplier search
                  mySupplier.find(function(err, suppliers) {
                      if (err) {
                          response.send(501, 'There was an error');
                      } else {
                          // render the response with suppliers and location
                          response.render('index', {
                              suppliers: suppliers,
                              locations: locations
                          });
                      }
                  });
      
              });
          }
      );
      

      无论您要查询位置和供应商,您都可能希望编写一个查询,以便在一次行程中检索位置和供应商。然后你就可以摆脱第二个异步调用了。

      【讨论】:

      • 感谢 Jim 对新手的帮助。这正是我所需要的。
      • 没问题。 Strongloop(现在维护 Express.js 的公司)有一篇关于管理回调的非常好的文章:strongloop.com/strongblog/…。这并不真正适用于您当前的情况,因为您只有 2 个延续。但是,随着您的逻辑变得更加复杂,您可能需要考虑其他方法来使您的代码更易于遵循。
      猜你喜欢
      • 2019-05-14
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2012-02-23
      • 2021-08-11
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      相关资源
      最近更新 更多