“未定义”使用多个callback函数(Node + EJS)

在我的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中显示位置和供应商的所有信息。 我可以同时工作,但不能同时工作。 我假设我做错了多个callback函数“/”。

这是我的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) 

你的问题是你没有使用“多个callback函数”你的根路线在这里。 以下是您的代码如何执行:

 // 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 }); 

我会build议尝试以下内容:

 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 }); } }); }); } ); 

但是,您正在查询位置和供应商,您可能需要编写一个查询,以便在一次旅行中检索位置和供应商。 那么你可以摆脱第二次asynchronous调用。

 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(); }); 

你可以试试这个方法。