javascript – 抛出新的TypeError(‘回调提供给同步glob’)?

前端之家收集整理的这篇文章主要介绍了javascript – 抛出新的TypeError(‘回调提供给同步glob’)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
执行错误的详细信息:

#node app.js

  1. throw new TypeError('callback provided to sync glob')
  2. ^
  3. TypeError: callback provided to sync glob
  4. at glob (C:\Users\z\Documents\node_modules\glob\glob.js:70:13)
  5. at Object.module.exports.getGlobbedFiles (C:\Users\z\Documents\Server\Config\config.js:31:4)
  6. at Object.<anonymous> (C:\Users\z\Documents\Server\app.js:102:10)

我正在使用glob 5.0.14来启动meanjs app.

这是我的config.js:

  1. var _ = require('lodash'),glob = require('glob');
  2.  
  3. module.exports.getGlobbedFiles = function(globPatterns,removeRoot) {
  4. var _this = this;
  5.  
  6. var urlRegex = new RegExp('^(?:[a-z]+:)?\/\/','i');
  7.  
  8. var output = [];
  9. if (_.isArray(globPatterns)) {
  10. globPatterns.forEach(function(globPattern) {
  11. output = _.union(output,_this.getGlobbedFiles(globPattern,removeRoot));
  12. });
  13. } else if (_.isString(globPatterns)) {
  14. if (urlRegex.test(globPatterns)) {
  15. output.push(globPatterns);
  16. } else {
  17. 31=> glob(globPatterns,{
  18. sync: true
  19. },function(err,files) {
  20. if (removeRoot) {
  21. files = files.map(function(file) {
  22. return file.replace(removeRoot,'');
  23. });
  24. }
  25. output = _.union(output,files);
  26. });
  27. }
  28. }
  29. return output;
  30. };

和app.js第102行:

  1. config.getGlobbedFiles('./Rutas/*.js').forEach(function(routePath) {
  2. require(path.resolve(routePath))(app);
  3. });

解决方法

就像我说的那样,你将回调参数传递给同步调用,将其更改为工作异步,或者删除回调参数:
  1. ...
  2. else {
  3. var files = glob(globPatterns,{ sync: true });
  4. if (removeRoot) {
  5. files = files.map(function(file) {
  6. return file.replace(removeRoot,'');
  7. });
  8. }
  9. output = _.union(output,files);
  10. }
  11. ...

猜你在找的JavaScript相关文章