【问题标题】:ReferenceError: app is not defined: node.jsReferenceError:未定义应用程序:node.js
【发布时间】:2019-12-31 11:05:48
【问题描述】:

我想要获取仪表板页面,但由于无法解决的问题而无法获取,我可以在这里获得帮助

用户控制器:

     const express = require('express');
     const router = express.Router();
     const mongoose = require('mongoose');
     const session = require('express-session');
     const passport = require('passport');
     const User = mongoose.model('User');

   app.router('/dashboard', (req, res) => { 
console.log("test");
User.find((err, docs) => { 
    if (!err) { res.render("dashboard", {
                   users: docs
               });
     } else { 
        console.log('Error in retrieving users list :' + err);
         } 
     }); 
  });

app.js 中的路由

   app.use('dashboard', userController);

dashboard.ejs:

    <div class="form-group">
<label for="exampleInputPassword1">users</label>
                                                                                                                          <% users.forEach(function (users) { %>
    <select class="form-control" id="exampleInputEmail1">
       <option><%= users.name %></option>
    </select>
    <% }) %>
  </div>

错误:

   the options [userNewParser] is not supported
   D:\nodejs\node_app\controllers\userController.js:10
   app.router('/dashboard', (req, res) => {
   ^

   ReferenceError: app is not defined

dashboard.ejs:

    <div class="form-group">
        <label for="exampleInputPassword1">users</label>
        <% users.forEach(function (users) { %>
        <select class="form-control" id="exampleInputEmail1">
          <option><%= users.name %></option>
        </select>
        <% }) %>
      </div>

app.js

   paste.ofcode.org/7zJVKxxhRan2KgskF37ixL

【问题讨论】:

  • 看看“Hello World”example
  • app 定义在哪里?
  • 检查更新问题中的错误
  • @RolandStarke 你能告诉我如何解决它,因为它是 php 的新手
  • 错误信息非常清楚。 app 变量没有定义,所以显然你不能使用它。检查第一条评论中链接中的示例,看看你应该如何定义它。

标签: node.js ejs node-modules


【解决方案1】:

再试一次

用户控制器:

  const express = require('express');
  const router = express.Router();
  const mongoose = require('mongoose');
  const session = require('express-session');
  const passport = require('passport');
  const User = mongoose.model('User');

  router.get('/dashboard', (req, res) => { 
    console.log("test");
    User.find((err, docs) => { 
    if (!err) { res.render("dashboard", {
                 users: docs
             });
    } else { 
      console.log('Error in retrieving users list :' + err);
       } 
    }); 
  });

  module.exports = router;

app.js:

  var userControler = require('./userControler.js');
  // ...
  app.use('/dashboard', userControler);

【讨论】:

  • D:\nodejs\node_app\node_modules\express\lib\router\index.js:160 req.next = next; ^ TypeError: Cannot create property 'next' on string 'dashboard' at Function.handle (D:\nodejs\node_app\node_modules\express\lib\router\index.js:160:12) at router (D:\nodejs\ node_app\node_modules\express\lib\router\index.js:47:12) 在 Object. (D:\nodejs\node_app\controllers\userController.js:9:1)
  • 通过使用 /dashboard 或使用仪表板相同的结果
  • router.get('/dashboard', (req, res) => { // ... });
  • 那个被删除了,现在它说:ReferenceError: D:\nodejs\node_app\views\dashboard.ejs:13 11|
    12| >> 13| 14|
  • 你能从问题中看到吗
【解决方案2】:

你可以用express直接调用路由器。

`const router = express();`

你不需要使用const router = express.Router();

【讨论】:

  • 如果可能,请努力提供额外的解释,而不仅仅是代码。此类答案往往更有用,因为它们可以帮助社区成员,尤其是新开发人员更好地理解解决方案的推理,并有助于避免需要解决后续问题。
猜你喜欢
相关资源
最近更新 更多
热门标签