NodeJs循环加载路由:无法读取未定义的属性'indexOf'

时间:2016-04-25 22:45:07

标签: javascript node.js express

尝试使用循环加载routes文件夹中的所有路径文件,我收到此错误:

  

无法读取未定义

的属性'indexOf'

代码:

77 var routes = {};
78 var routeFiles = fs.readdirSync('./routes');
79 for(var i = 0; i < routeFiles.length; i++){
80    if (routeFiles[i].match(/\.js$/) !== null && routeFiles[i] !== 'index.js') {
81        var name = routeFiles[i].replace('.js', '');
82        try {
83            routes[name] = require('./routes/' + name)(passport);
84        } catch (e) {
85            console.log(e.stack);
86        }
87    }
88}

错误出现在这一行:

routes[name] = require('./routes/' + name)(passport);

当我在没有循环的情况下编写它时它工作正常,例如:

var name = 'main';
routes[name] = require('./routes/' + name)(passport);

修改

添加了行号。

堆栈跟踪:

TypeError: Cannot read property 'indexOf' of undefined
at Function.handle (/home/burawi/Documents/Dev/daftar/node_modules/express/lib/router/index.js:140:27)
at router (/home/burawi/Documents/Dev/daftar/node_modules/express/lib/router/index.js:46:12)
at /home/burawi/Documents/Dev/daftar/app.js:83:59
at tryCatch1 (/home/burawi/Documents/Dev/daftar/node_modules/sequelize-bluebird/js/main/util.js:43:21)
at Promise$_callHandler [as _callHandler] (/home/burawi/Documents/Dev/daftar/node_modules/sequelize-bluebird/js/main/promise.js:639:13)

at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/home/burawi/Documents/Dev/daftar/node_modules/sequelize-bluebird/js/main/promise.js:653:18)
at Promise$_settlePromiseAt [as _settlePromiseAt] (/home/burawi/Documents/Dev/daftar/node_modules/sequelize-         bluebird/js/main/promise.js:817:14)
at Promise$_settlePromises [as _settlePromises] (/home/burawi/Documents/Dev/daftar/node_modules/sequelize-  bluebird/js/main/promise.js:951:14)
at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/home/burawi/Documents/Dev/daftar/node_modules/sequelize-bluebird/js/main/async.js:75:12)
at Async$consumeFunctionBuffer (/home/burawi/Documents/Dev/daftar/node_modules/sequelize-bluebird/js/main/async.js:38:14)

0 个答案:

没有答案