私は本当に奇妙な問題を襲ってきたの異なるオブジェクト。引数は、ノード& Expressで、引数
私は3つの異なる方法で呼び出すことができますいずれかのミドルウェア機能、持っている:私は、しかし
exports.auth = (a, b, c) => {
let role, scope;
switch(arguments.length) {
case 3:
// Called directly by express router
handleAuth(a, b, c);
break;
case 2:
case 1:
// Called with role/scope, return handler
// but don't execute
role = a;
scope = b;
return handleAuth;
}
function handleAuth(req, res, next) {
// Do some custom auth handling, can
// check here if role/scope is set
return next();
}
}
:これを行うには明白な方法は次のようになります
app.get('/', auth, handler)
app.get('/', auth('role'), handler)
app.get('/', auth('role', 'scope'), handler)
をarguments.length
の非常に奇妙な結果が得られました。第2の方法で呼び出されたときは、arguments.length == 5
であり、引数のいずれもrole
ではありません。
[ '0': '[object Object]',
'1': 'function require(path) {\n try {\n exports.requireDepth += 1;\n return mod.require(path);\n } finally {\n exports.requireDepth -= 1;\n }\n }',
'2': '[object Object]',
'3': '/Users/redacted/auth/index.js',
'4': '/Users/redacted/auth' ]
私は関数内a, b, c
をログインした場合、私はa: 'role', b: undefined, c: undefined
を取得します。
私はは、runkitでそれを再現しようとしたが、任意の運を持っていなかった。https://runkit.com/benedictlewis/5a2a6538895ebe00124eb64e