请求= {名称:url,name:url,...}我已尝试获取每个URL并返回{ name:数据,名称:数据,...}
代码
async function fetchMovies(url: string) {
const data = await fetch(baseURL + url).then((r) => r.json());
return data.results;
}
export const getMovies = createAsyncThunk("movies/getMovies", async () => {
const responses = Object.entries(requests).map(async ([key, value]) => {
try {
const response = await fetchMovies(value);
return [key, response];
} catch (e: unknown) {
if (e instanceof Error) {
console.log(e.message);
}
}
});
const results = await Promise.all(responses);
console.log(results); // [[string, SomeType[]], [string, SomeType[]], ...]
return Object.fromEntries(results); // error
});
控制台日志(结果)
Array (8)
0 ["netflixOriginals", Array] (2)
1 ["trending", Array] (2)
2 ["topRated", Array] (2)
3 ["action", Array] (2)
4 ["comedy", Array] (2)
5 ["horror", Array] (2)
6 ["romance", Array] (2)
7 Array (2)
0 "documentary"
1 [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, …] (20)
错误
TS2769: No overload matches this call.
Overload 1 of 2, '(entries: Iterable<readonly [PropertyKey, any]>): { [k: string]: any; }', gave the following error.
Argument of type '(any[] | undefined)[]' is not assignable to parameter of type 'Iterable<readonly [PropertyKey, any]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<any[] | undefined, any>' is not assignable to type 'IteratorResult<readonly [PropertyKey, any], any>'.
Type 'IteratorYieldResult<any[] | undefined>' is not assignable to type 'IteratorResult<readonly [PropertyKey, any], any>'.
Type 'IteratorYieldResult<any[] | undefined>' is not assignable to type 'IteratorYieldResult<readonly [PropertyKey, any]>'.
Type 'any[] | undefined' is not assignable to type 'readonly [PropertyKey, any]'.
Type 'undefined' is not assignable to type 'readonly [PropertyKey, any]'.
Overload 2 of 2, '(entries: Iterable<readonly any[]>): any', gave the following error.
Argument of type '(any[] | undefined)[]' is not assignable to parameter of type 'Iterable<readonly any[]>'.
The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
Type 'IteratorResult<any[] | undefined, any>' is not assignable to type 'IteratorResult<readonly any[], any>'.
Type 'IteratorYieldResult<any[] | undefined>' is not assignable to type 'IteratorResult<readonly any[], any>'.
Type 'IteratorYieldResult<any[] | undefined>' is not assignable to type 'IteratorYieldResult<readonly any[]>'.
Type 'any[] | undefined' is not assignable to type 'readonly any[]'.
Type 'undefined' is not assignable to type 'readonly any[]'.
22 | const results = await Promise.all(responses);
23 | console.log(results);
> 24 | return Object.fromEntries(results);
| ^^^^^^^
25 | });
26 |
我找到的唯一解决方案是结果:任意[]。我不是很擅长TS,我已经尝试了我知道的一切。抱歉问了这个愚蠢的问题
1条答案
按热度按时间6kkfgxo01#
由于您没有为
responses
对象指定type,它将根据其操作返回的结果自动进行检测。就你而言,
因此,响应类型应为
(any[] | undefined)[]
对此做一些处理,(比如也在catch中返回[key,value]),那么类型将是
any[][]
。Object.fromEntries()将接受此类型。