我在下面的代码:

const minLocationLength = 1;
        if (location.description.length < minLocationLength) {
          reject(`Location must be at least ${minLocationLength} characters.`);
        }


我收到错误Cannot read property 'length' of undefined。我做对了吗?

最佳答案

description的值未定义,您应首先进行如下检查:

 if (location.description && location.description.length < minLocationLength) {
          reject(`Location must be at least ${minLocationLength} characters.`);
        }

09-18 17:09