无法读取finalHandler节点模块中未定义的属性'_header'

时间:2017-05-30 16:13:36

标签: node.js node-modules

我似乎无法找到解决问题的方法。我正在尝试启动我的nodeJS / expressJS应用程序但是在“npm start”时收到错误。

错误&堆栈跟踪:

+-----------+----------+----------+-----+------------+
| RequestNo | UserName |  Action  | Seq | ActionDate |
+-----------+----------+----------+-----+------------+
| R001      | JackF    | Approver |   5 | NULL       |
| R001      | RobertL  | Approver |   6 | NULL       |
+-----------+----------+----------+-----+------------+

违规代码(在node_modules / finalhandler / index.js中):
(注意:我自己添加了console.logs,它们不是模块的一部分)

    /Users/jmccreary/Documents/NewHireSite/node_modules/finalhandler/index.js:94
    if (!err && res._header) {
                   ^

TypeError: Cannot read property '_header' of undefined
    at /Users/jmccreary/Documents/NewHireSite/node_modules/finalhandler/index.js:94:20
    at /Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/router/index.js:635:15
    at next (/Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/router/index.js:210:14)
    at Function.handle (/Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/router/index.js:174:3)
    at Function.handle (/Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/application.js:174:10)
    at app (/Users/jmccreary/Documents/NewHireSite/node_modules/express/lib/express.js:38:9)
    at Object.<anonymous> (/Users/jmccreary/Documents/NewHireSite/app.js:101:25)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/jmccreary/Documents/NewHireSite/bin/www:7:11)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)

从我的console.log()中可以看出,输出如下:

  function finalhandler (req, res, options) {
  var opts = options || {}
  console.log("The req is: ", req);
  console.log("The res is: ", res);

  // get environment
  var env = opts.env || process.env.NODE_ENV || 'development'

  // get error callback
  var onerror = opts.onerror

  return function (err) {
    var headers
    var msg
    var status

    // ignore 404 on in-flight response
    if (!err && res._header) {
      debug('cannot 404 after headers sent')
      return
    }
    ...

编辑:我相信这是调用错误的代码(来自node_modules / express / lib / application.js):

 The req is:  Authenticator {
  _key: 'passport',
  _strategies: { session: SessionStrategy { name: 'session' } },
  _serializers: [],
  _deserializers: [],
  _infoTransformers: [],
  _framework: 
   { initialize: [Function: initialize],
     authenticate: [Function: authenticate] },
  _userProperty: 'user',
  Authenticator: [Function: Authenticator],
  Passport: [Function: Authenticator],
  Strategy: { [Function: Strategy] Strategy: [Circular] },
  strategies: { SessionStrategy: { [Function: SessionStrategy] super_: [Object] } } }
 The res is:  undefined

我的package.json文件(使用Express 4)如下:

 /**
 * Dispatch a req, res pair into the application. Starts pipeline processing.
 *
 * If no callback is provided, then default error handlers will respond
 * in the event of an error bubbling through the stack.
 *
 * @private
 */

app.handle = function handle(req, res, callback) {
  var router = this._router;

  // final handler
  var done = callback || finalhandler(req, res, {
    env: this.get('env'),
    onerror: logerror.bind(this)
  });

  // no routes
  if (!router) {
    debug('no routes defined on app');
    done();
    return;
  }

  router.handle(req, res, done);
};

我能够很好地运行教程的源代码,并且它与我的应用程序具有相同的模块,几乎完全相同(除了在npm start命令上的www / bin文件)。我已经尝试删除其他模块并在我的app.js文件中移动东西无济于事。

任何指针或帮助都将非常感激。谢谢!

2 个答案:

答案 0 :(得分:4)

我有同样的错误。以下为我解决了问题,也许它适合你?我有这个代码:

var express = require('express')();
var app = express();

app.listen(3000, function () {
  console.log('Example app listening at http://%s:%s');
});

它有一个()太多了。删除它解决了这个问题:

var express = require('express');

答案 1 :(得分:0)

我刚才有这个问题。当我开始清除代码时,一切都开始发生,例如删除“;”在行尾。我带回其中一个“;”问题消失了。