Javascript 分析多个对象属性时出错';长度';不存在
对于由事件调用的函数,我有以下定义Javascript 分析多个对象属性时出错';长度';不存在,javascript,node.js,typescript,Javascript,Node.js,Typescript,对于由事件调用的函数,我有以下定义 forceUpdateLiveLocation: Returns Promise<false | true | LiveLocationChangedEvent[]> const forcedLiveLocation = await client.forceUpdateLiveLocation( message.from ); console.log(forcedLiveLocation[0]); 当我有几个事件时,他
forceUpdateLiveLocation: Returns Promise<false | true | LiveLocationChangedEvent[]>
const forcedLiveLocation = await client.forceUpdateLiveLocation( message.from );
console.log(forcedLiveLocation[0]);
当我有几个事件时,他会构建如下对象:
[ {
lat: -2.510491,
lng: -44.220796
},{
lat: -2343434,
lng: -443434343
}];
因此,我尝试对对象执行,但我有一个错误
for(var i = 0; i < forcedLiveLocation.length; i++){
console.log(forcedLiveLocation[i].lat.toString());
}
for(变量i=0;i
错误:
类型“boolean | LiveLocationChangedEvent[]”上不存在属性“length”。
类型“false”上不存在属性“length”
承诺应该返回以下类型之一bool
或LiveLocationChangedEvent[]
,因此您需要处理forceUpdateLiveLocation
函数的类型
if(typeof forcedLiveLocation !== "boolean") // then do what ever you want
记录结果之所以有效,是因为console.log
与返回的类型无关据我猜测,文档位于:
聊天参与者的列表,这些参与者的实时位置位于。如果
聊天室不存在,或聊天室没有任何联系人
积极分享他们的现场位置,它将返回false。如果它是一个
与单个联系人聊天,如果
联系人的Live Location已打开。请注意。这应该只是
每30秒左右呼叫一次。这会迫使手机抓取数据
号码的最新实时位置数据。这可以用于
与onLiveLocation结合(这将触发onLiveLocation)
所以,有时你会得到一系列事件,有时你不会。你需要对不同的情况做出相应的反应
if (Array.isArray(forcedLiveLocation)){
for(var i = 0; i < forcedLiveLocation.length; i++){
console.log(forcedLiveLocation[i].lat.toString());
}
} else {
// do something else
}
if(Array.isArray(forcedLiveLocation)){
对于(变量i=0;i
尝试打印console.log(forcedLiveLocation)代码>完美它甚至解决了我在setInterval上遇到的一个问题,谢谢
if (Array.isArray(forcedLiveLocation)){
for(var i = 0; i < forcedLiveLocation.length; i++){
console.log(forcedLiveLocation[i].lat.toString());
}
} else {
// do something else
}