node.js - "Not defined"使用多个回调函数(Node + EJS)

标签 node.js mongodb mongoose 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 中显示位置和供应商的所有信息。我可以让两者单独工作,但不能同时进行。我假设我对“/”的多个回调函数做错了。

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

        });
    }
);

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

关于node.js - "Not defined"使用多个回调函数(Node + EJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28654079/

相关文章:

javascript - 如何从 JSON 对象中获取匹配的键值?

node.js - 使用来自远程服务器的 Node js 显示文本文件

javascript - 仅更新 MongoDB 中的某些子文档

node.js - 如何在 Mongoose 中使用聚合

node.js - 无法在 graphQL 中使用 populate 获取嵌套 Mongoose 查询

javascript - 如何在 DELETE 路由中发送消息 - express.js

javascript - TypeScript:基于对象创建界面

java - spring data 和 mongodb 中级联保存

javascript - Mongodb,如何对1个对象进行多个请求

node.js - 如何在 Mongoose 查询中使用 if 语句?