当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How do I read the length of a value within an object?

问题描述:

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.`);
        }
分享给朋友:
您可能感兴趣的文章:
随机阅读: