我的代码工作正常,但现在我收到错误属性查找在类型上不存在
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"
如何解决这个问题?
在您的代码中,
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
在某些情况下会返回非数组数据。