问题描述:

I have this code below:

const minLocationLength = 1;

if (location.description.length < minLocationLength) {

reject(`Location must be at least ${minLocationLength} characters.`);

}

I get the error Cannot read property 'length' of undefined. Am I doing it right?

网友答案:

The value of description is not defined, you shall check like this first:

 if (location.description && location.description.length < minLocationLength) {
          reject(`Location must be at least ${minLocationLength} characters.`);
        }
相关阅读:
Top