我的代码如下:
const minLocationLength = 1;
if (location.description.length < minLocationLength) {
reject(`Location must be at least ${minLocationLength} characters.`);
}
我收到错误Cannot read property 'length' of undefined
。我做得对吗?
答案 0 :(得分:1)
未定义描述的值,您应首先检查:
if (location.description && location.description.length < minLocationLength) {
reject(`Location must be at least ${minLocationLength} characters.`);
}