Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Code

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

Error

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

TypeError: Cannot match against 'undefined' or '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.

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

like image 245
Prashanth Chandra Avatar asked May 26 '16 06:05

Prashanth Chandra


1 Answers

this error should only arise if the array or object being destructured or its children is undefined or 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'.
like image 179
thefourtheye Avatar answered Oct 31 '22 07:10

thefourtheye