【问题标题】:Getting "Error: could not parse function arguments ()=>"得到“错误:无法解析函数参数()=>”
【发布时间】:2017-11-04 11:41:03
【问题描述】:

以下是我的代码:-

projectDependencies.forEach((val)=>
{
    container.register(val[0],function ()
    {
        return require(val[1]);
    });
});

当我运行命令 nodemon server.js 时,我收到以下错误:

Error: could not parse function arguments: ()=>
{
    return container;
}
    at argList (C:\Users\Data-Vinci\Desktop\JS\chatapp\node_modules\dependable\index.js:97:15)
    at toFactory (C:\Users\Data-Vinci\Desktop\JS\chatapp\node_modules\dependable\index.js:82:21)
    at registerOne (C:\Users\Data-Vinci\Desktop\JS\chatapp\node_modules\dependable\index.js:33:32)
    at Object.register (C:\Users\Data-Vinci\Desktop\JS\chatapp\node_modules\dependable\index.js:26:16)
    at Object.<anonymous> (C:\Users\Data-Vinci\Desktop\JS\chatapp\container.js:22:11)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)

我认为我的代码在语法上是正确的。不知道问题出在哪里。

注意:- 但是,如果我使用 function() 而不是 ()=> 则一切正常。

【问题讨论】:

    标签: javascript node.js express ecmascript-6


    【解决方案1】:

    dependable.argList 将函数转换为字符串并解析出 arg 名称。箭头函数不会字符串化到它正在寻找的内容。

    https://github.com/Schoonology/dependable/blob/master/index.coffee#L63

    match = func.toString().match /function.*?\(([\s\S]*?)\)/
    if not match? then throw new Error "could not parse function arguments: #{func?.toString()}"
    

    例如

    > func = function(val){ console.log(val); }
    > func.toString()
    'function (val){ console.log(val); }'
    
    > arrowFunc = (val) => console.log(val);
    > arrowFunc.toString();
    '(val) => console.log(val)'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多