TypeError:无法匹配'undefined'或'null' [英] TypeError: Cannot match against 'undefined' or 'null'

查看:131
本文介绍了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 <时才会出现此错误/ code>或 null

Why am I getting this error? My knowledge of ES6 led me to presume that this error should only arise if the array or object being destructured or its children is undefined or null.

我不知道函数参数用作匹配项如果它们是,那么为什么只是一个错误,如果我试图去构造其中一个?(那不是未定义 null )。

I wasn't aware that function parameters are used as a match. And if they are then why is it only an error if I try to destructure one of them? (that isn't undefined or null).

解决方案


只有在被解构的数组或对象或其子节点 undefined 时才会出现此错误 null

完全正确。在您的情况下,被解构的对象是 undefined null 。例如,

Exactly. In your case, the object being destructured is either undefined or null. For example,

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

这篇关于TypeError:无法匹配'undefined'或'null'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆