TS2339:类型上不存在属性查找[已关闭]

问题描述 投票:0回答:1

我的代码工作正常,但现在我收到错误属性查找在类型上不存在

async function getDetails(id){
    const userKey =1;
    const test = await getUserDetails(id);
     type of test will be Array of Object
    test.find((data) => data.key === userKey);
   // In above line I am getting error Property find does not exist on type
}
await getDetails(2);

我已将其添加到 tsconfig.json 但仍然遇到相同的错误。

"lib":["es6"],
"target":"es5"

如何解决这个问题?

javascript typescript ecmascript-6
1个回答
0
投票

在您的代码中,

test.find
const test = Some db call
之前被调用,因为
const test = Some db call
需要一些时间。 所以,当调用
test.find
时,
test
undefined
,而不是数组。

要解决这个问题,您必须创建异步函数,然后更改代码,如下所示。例如...

async func = ()=>{
  const userKey =1;
  const test = await getUserDetails(id);
      //type of test will be Array of Object
  [...test].find((data) => data.key === userKey);
}

我认为

getUserDetails
在某些情况下会返回非数组数据。

© www.soinside.com 2019 - 2024. All rights reserved.