代码
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
)。
最佳答案
this error should only arise if the array or object being destructured or its children is
undefined
ornull
.
没错。在您的情况下,被解构的对象是 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'.
关于javascript - 类型错误 : Cannot match against 'undefined' or 'null' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37453235/