2016-05-26 11 views
36

'未定義' や 'NULL' コードはTypeError:

client.createPet(pet, (err, {name, breed, age}) => { 
    if (err) { 
    return t.error(err, 'no error') 
    } 
    t.equal(pet, {name, breed, age}, 'should be equivalent') 
}) 

エラー

client.createPet(pet, (err, {name, breed, age}) => { 
         ^

TypeError: Cannot match against 'undefined' or 'null'. 

照合することはできませんなぜ私はこのエラーを取得していますか?私のES6について知ったところでは、配列またはオブジェクトが破壊されている場合またはその子undefinedまたはnullの場合にのみ、このエラーが発生するはずです。

私は、機能パラメータがマッチとして使用されていることに気づいていませんでした。そして、もし彼らがそうであれば、なぜ私はそれらの一つを破壊しようとするとエラーになるのですか? (これはundefinedまたはnullではありません)。

答えて

53

this error should only arise if the array or object being destructured or its children is undefined or null .

正確に。あなたのケースでは、破棄されるオブジェクトはundefinedまたはnullです。例えば、

function test(err, {a, b, c}) { 
    console.log(err, a, b, c); 
} 

test(1, {a: 2, b: 3, c: 4}); 
// 1 2 3 4 
test(1, {}); 
// 1 undefined undefined undefined 
test(1); 
// TypeError: Cannot match against 'undefined' or 'null'. 
+0

あなたは正しいですが、私はあなたがよく見ると、矢印が実際に左括弧を指すエラーメッセージ –

+0

@PrashanthChandraの矢印に惑わさしまった、それはAです:-) – thefourtheye

+1

エラーではありません最近リリースされた機能にはもっと有益なメッセージがありません。 – Elad