执行错误的详细信息:
#node app.js
throw new TypeError('callback provided to sync glob')
^
TypeError: callback provided to sync glob
at glob (C:\Users\z\Documents\node_modules\glob\glob.js:70:13)
at Object.module.exports.getGlobbedFiles (C:\Users\z\Documents\Server\Config\config.js:31:4)
at Object.<anonymous> (C:\Users\z\Documents\Server\app.js:102:10)
我正在使用glob 5.0.14来启动meanjs app.
这是我的config.js:
var _ = require('lodash'),glob = require('glob');
module.exports.getGlobbedFiles = function(globPatterns,removeRoot) {
var _this = this;
var urlRegex = new RegExp('^(?:[a-z]+:)?\/\/','i');
var output = [];
if (_.isArray(globPatterns)) {
globPatterns.forEach(function(globPattern) {
output = _.union(output,_this.getGlobbedFiles(globPattern,removeRoot));
});
} else if (_.isstring(globPatterns)) {
if (urlRegex.test(globPatterns)) {
output.push(globPatterns);
} else {
31=> glob(globPatterns,{
sync: true
},function(err,files) {
if (removeRoot) {
files = files.map(function(file) {
return file.replace(removeRoot,'');
});
}
output = _.union(output,files);
});
}
}
return output;
};
和app.js第102行:
config.getGlobbedFiles('./Rutas/*.js').forEach(function(routePath) {
require(path.resolve(routePath))(app);
});
解决方法
就像我说的那样,你将回调参数传递给同步调用,将其更改为工作异步,或者删除回调参数:
...
else {
var files = glob(globPatterns,{ sync: true });
if (removeRoot) {
files = files.map(function(file) {
return file.replace(removeRoot,'');
});
}
output = _.union(output,files);
}
...