繁体   English   中英

异步/等待无法在地图功能内正常工作

[英]Async/await not working as expected inside map function

我试图在map函数中执行多个get请求,但是我正在' 用“ TypeError:无法读取未定义的属性'trackItems' ' 捕获了未处理的诺言拒绝

波纹管功能基本上是将响应作为参数接收,并建立一个对象以保存其内容。 最后,我按预期使用id,name和type构建了对象,但trackItems数组始终为空 我确信我从getPlaylistTracks函数中得到的内容是正确的,并且无法理解为什么它无法正常工作。

另外,我在getPlaylistTracks上使用requestPromiseNative来执行请求并使用了await,而我的homeContentItems全局变量具有以下类型的结构:

private static homeContentItems :any = {项目:[{内容:{项目:[]}}]};

这是saveContent函数:

   private saveContent(homeItemsResponse: any): any {
        try {
          if (homeItemsResponse && homeItemsResponse !== undefined) {
            homeItemsResponse.map((playlistCategoriesItem: any, i: number) => {
              const homeContent: any = Home.homeContentItems.items[0].content.items;

              homeContent.push({ name: playlistCategoriesItem.name, content: { items: [] } });

              const playlistContent: any = playlistCategoriesItem.content.items;

              playlistContent.map(async (playlistItem: any) => {
                const homePlaylistItems: any = Home.homeContentItems.items[0].content.items[i].content.items;

                homePlaylistItems.push({
                  id: playlistItem.id,
                  name: playlistItem.name,
                  type: playlistItem.type,
                  trackItems: [],
                });

                const playlistId: string = playlistItem.id;
                const response: any = await this.getPlaylistTracks(playlistId);

                response.items.map(async (item: any) => {
                  homePlaylistItems.trackItems.push(item);
                });
              });
            });
          }
        } catch (error) {
          this.logger.info(`@@@@ Home - Error on saveHomeContent: ${error}`);
        }
      }

先感谢您 :)

使用for循环代替。 这样, await将按预期工作。

同样不要忘记使saveContent函数async

private async saveContent(homeItemsResponse: any[]): any {
  try {
    if (homeItemsResponse) {
      let i = 0;

      for (const playlistCategoriesItem: any of homeItemsResponse) {
        const homeContent: any = Home.homeContentItems.items[0].content.items;

        homeContent.push({ name: playlistCategoriesItem.name, content: { items: [] } });

        const playlistContent: any = playlistCategoriesItem.content.items;

        for (const playlistItem: any of playlistContent) {
          const homePlaylistItems: any = Home.homeContentItems.items[0].content.items[i].content.items;

          homePlaylistItems.push({
            id: playlistItem.id,
            name: playlistItem.name,
            type: playlistItem.type,
            trackItems: [],
          });

          const playlistId: string = playlistItem.id;
          const response: any = await this.getPlaylistTracks(playlistId);

          for (const item: any of response.items) {
            homePlaylistItems.trackItems.push(item);
          }
        }

        i++;
      }
    }
  } catch (error) {
    this.logger.info(`@@@@ Home - Error on saveHomeContent: ${error}`);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM