gpt4 book ai didi

javascript - 如何读取对象内值的长度?

转载 作者:行者123 更新时间:2023-12-03 06:37:18 24 4
gpt4 key购买 nike

我有下面的代码:

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

我收到错误无法读取未定义的属性“长度”。我这样做对吗?

最佳答案

description的值没有定义,你应该先这样检查:

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

关于javascript - 如何读取对象内值的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com