ReferenceError:应用程序未定义:node.js

时间:2019-08-27 09:37:13

标签: node.js ejs node-modules

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

userControler:

     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

2 个答案:

答案 0 :(得分:1)

再试一次

userControler:

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

答案 1 :(得分:0)

您可以直接用Express呼叫路由器。

`const router = express();`

您不需要用作const router = express.Router();