在我的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)
答案 0 :(得分:1)
您的问题是您没有使用多个回调功能&#39;在这里你的根路线。以下是您的代码执行方式:
// 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
});
}
});
});
}
);
但是,您要查询位置和供应商,您可能希望编写一个查询,以便在一次旅行中检索位置和供应商。然后你可以摆脱第二次异步调用。
答案 1 :(得分:0)
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();
});
您可以尝试这种方法。