TypeError:无法匹配'undefined'或'null'

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才会出现此错误。

我不知道函数参数是用来匹配的。 如果他们是那么为什么如果我试图解构他们其中之一只是一个错误? (这不是undefinednull )。

只有当被解构的数组或者对象或者它的子undefinedundefined或者null才会出现这个错误。

究竟。 在你的情况下,被解构的对象可能是undefinednull 。 例如,

 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'.