向现有项目添加了护照身份验证,现在出现 Cannot read 属性 of undefined 错误

Added passport authentication to an existing project and now get a Cannot read property of undefined error

我有一个简单的 node/express/knex 应用程序,可以读取和编辑 postgres 数据库。我用 auth0 添加了护照身份验证,一切都很顺利。当我注意到我实际上无法再提交更改时,我正要结束这一天。从数据库中读取的所有页面都可以正常工作,但是当我进行更改并调用 router.post 路由时,我会收到这样的错误。奇怪的是那段代码绝对没有任何改变。发生在三个不同的路线上。

TypeError: Cannot read property 'amp30' of undefined
   at /app/kml/index.js:431:22
   at Layer.handle [as handle_request] (/app/kml/node_modules/express/lib/router/layer.js:95:5)
   at next (/app/kml/node_modules/express/lib/router/route.js:131:13)
   at Route.dispatch (/app/kml/node_modules/express/lib/router/route.js:112:3)
   at Layer.handle [as handle_request] (/app/kml/node_modules/express/lib/router/layer.js:95:5)
   at /app/kml/node_modules/express/lib/router/index.js:277:22
   at Function.process_params (/app/kml/node_modules/express/lib/router/index.js:330:12)
   at next (/app/kml/node_modules/express/lib/router/index.js:271:10)
   at Function.handle (/app/kml/node_modules/express/lib/router/index.js:176:3)
   at router (/app/kml/node_modules/express/lib/router/index.js:46:12)
   at Layer.handle [as handle_request] (/app/kml/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/app/kml/node_modules/express/lib/router/index.js:312:13)
   at /app/kml/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/app/kml/node_modules/express/lib/router/index.js:330:12)
   at next (/app/kml/node_modules/express/lib/router/index.js:271:10)
   at SessionStrategy.strategy.pass (/app/kml/node_modules/passport/lib/middleware/authenticate.js:325:9)

这里以整条路线为例

router.post('/app/post/hydrant', function(req, res) {

  // Update hydrants based on data passed as POST to url/app/app-hydrants-single-edit-post
  knex('hydrants')
    .withSchema('snowmaking')
    .update({
      '30a': req.body.amp30,
      '60a': req.body.amp60,
      water: req.body.water,
      air: req.body.air,
      notes: req.body.notes,
      circuit: req.body.circuit,
      edit_by: req.body.edit_by
    })
    .where({
      id: req.body.id
    })
    .then(function(projectNames) {
      res.render('app-hydrants-single-edit-post');
    });

问题可能是因为正文解析器配置不正确。要配置主体解析器,请在您的项目中添加以下代码(app.js)

var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));