在我的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)
最佳答案
您的问题是您的根路由没有使用“多个回调函数”。这是执行代码的方式:
// 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
});
}
});
});
}
);
但是,如果要查询位置和供应商,则可能需要编写一个查询,以便一次查询检索位置和供应商。然后,您可以摆脱第二个异步调用。