Javascript I';我试图使用async/await获取服务,但第二个服务返回don';不要填充我的变量

Javascript I';我试图使用async/await获取服务,但第二个服务返回don';不要填充我的变量,javascript,angular,Javascript,Angular,我有一个从服务器获取列表的服务。但在此列表中,我需要调用另一个服务来返回徽标img,服务返回ok,但我的列表仍然为空。我做错了什么 我尝试在这两个服务中使用async/await 我尝试使用一个单独的函数来获取徽标,但是我的html没有改变 async getOpportunitiesByPage(_searchQueryAdvanced: any = 'active:true') { this.listaOportunidades = await this._opportunitie

我有一个从服务器获取列表的服务。但在此列表中,我需要调用另一个服务来返回徽标img,服务返回ok,但我的列表仍然为空。我做错了什么

我尝试在这两个服务中使用async/await 我尝试使用一个单独的函数来获取徽标,但是我的html没有改变

 async getOpportunitiesByPage(_searchQueryAdvanced: any = 'active:true') {
    this.listaOportunidades = await this._opportunities
      .listaOportunidades(this.pageSize, this.currentPage, _searchQueryAdvanced)
      .toPromise()
      .then(result => {
        this.totalSize = result['totalElements'];
        return result['content'].map(async (opportunities: any) => {
          opportunities.logoDesktopUrl = await this.getBrand(opportunities['brandsUuid']);
          console.log(opportunities.logoDesktopUrl);
          return { opportunities };
        });
      });

    this.getTasks(this.totalSize);
  }

没有错误,只是我的html没有改变。 在我的 console.log(opportunities.logodextopurl); 返回未定义

但最终回报率还是很高

信息: 角度7
服务器amazon aws。

等待
用于等待
承诺

如果要在
GetOpportunities ByPage
中等待,则应从
getBrand
返回
promise

更改
getBrand
功能,如下所示

getBrand(brandsUuid): Observable<string> {
  this.brandService.getById(brandsUuid).pipe(map(res => { 
    console.log(res.logoDesktopUrl); return res.logoDesktopUrl;
  }))
}
getBrand(brandsUuid):可观察{
this.brandService.getById(brandsUuid).pipe(map(res=>{
log(res.logoDesktopUrl);返回res.logoDesktopUrl;
}))
}
Change
opportunities.logodektopurl=等待这个.getBrand(opportunities['brandsUuid'])
to
opportunities.logoDesktopUrl=等待这个.getBrand(opportunities['brandsUuid')).toPromise()


请确保您从
rxjs/operators
导入了
map
wait
用于等待
promise

如果要在
GetOpportunities ByPage
中等待,则应从
getBrand
返回
promise

更改
getBrand
功能,如下所示

getBrand(brandsUuid): Observable<string> {
  this.brandService.getById(brandsUuid).pipe(map(res => { 
    console.log(res.logoDesktopUrl); return res.logoDesktopUrl;
  }))
}
getBrand(brandsUuid):可观察{
this.brandService.getById(brandsUuid).pipe(map(res=>{
log(res.logoDesktopUrl);返回res.logoDesktopUrl;
}))
}
Change
opportunities.logodektopurl=等待这个.getBrand(opportunities['brandsUuid'])
to
opportunities.logoDesktopUrl=等待这个.getBrand(opportunities['brandsUuid')).toPromise()


请确保您首先从
rxjs/operators

导入
map
,当您等待
时,不应使用
然后使用

第二步,
async/await
仅在承诺的情况下运行

async getOpportunitiesByPage(_searchQueryAdvanced: any = 'active:true') {
  const result = await this._opportunities
    .listaOportunidades(this.pageSize, this.currentPage, _searchQueryAdvanced)
    .toPromise();
  this.totalSize = result['totalElements'];
  this.listaOportunidades = result['content'].map(async (opportunities: any) => {
    opportunities.logoDesktopUrl = await this.getBrand(opportunities['brandsUuid']);
    console.log(opportunities.logoDesktopUrl);
    return opportunities;
  });

  this.getTasks(this.totalSize);
}

getBrand(brandsUuid) { 
  return new Promise((resolve, reject) => {
    this.brandService.getById(brandsUuid).subscribe(res => { 
      console.log(res.logoDesktopUrl);
      return resolve(res.logoDesktopUrl);
    }, err => {
      return reject(err);
    });
  });
}
但是,因为
rxjs
是Angular中使用的,所以应该使用它而不是
async/await

getOpportunitiesByPage: void(_searchQueryAdanced: any = 'active:true') {
  this._opportunities.listaOportunidades(this.pageSize, this.currentPage, _searchQueryAdvanced).pipe(
    tap(result => {
      // we do that here because the original result will be "lost" after the next 'flatMap' operation
      this.totalSize = result['totalElements'];
    }),
    // first, we create an array of observables then flatten it with flatMap
    flatMap(result => result['content'].map(opportunities => this.getBrand(opportunities['brandsUuid']).pipe(
        // merge logoDesktopUrl into opportunities object
        map(logoDesktopUrl => ({...opportunities, ...{logoDesktopUrl}}))
      )
    ),
    // then we make each observable of flattened array complete
    mergeAll(),
    // then we wait for each observable to complete and push each result in an array
    toArray()
  ).subscribe(
    opportunitiesWithLogoUrl => { 
      this.listaOportunidades = opportunitiesWithLogoUrl;
      this.getTasks(this.totalSize);
    }, err => console.log(err)
  );
}

getBrand(brandsUuid): Observable<string> {
  return this.brandService.getById(brandsUuid).pipe(
    map(res => res.logoDesktopUrl)
  );
}
getOpportunitiesByPage:void(\u searchQueryDanced:any='active:true'){
此.\u opportunities.ListaOpportUnidades(this.pageSize,this.currentPage,\u searchQueryAdvanced)。管道(
点击(结果=>{
//我们在这里这样做是因为在下一次“flatMap”操作后原始结果将“丢失”
this.totalSize=结果['totalElements'];
}),
//首先,我们创建一个观察数组,然后用flatMap将其展平
flatMap(结果=>result['content'].map(机会=>this.getBrand(机会['brandsUuid']).pipe(
//将logoDesktopUrl合并到opportunities对象中
映射(logoDesktopUrl=>({…机会,{logoDesktopUrl}}))
)
),
//然后,我们使平坦阵列的每个可观测值都是完整的
mergeAll(),
//然后我们等待每个可观察到的结果完成,并将每个结果推送到一个数组中
toArray()
).订阅(
LogoURL=>{
this.listaOportunidades=带有logourl的机会;
this.getTasks(this.totalSize);
},err=>console.log(err)
);
}
getBrand(brandsUuid):可观察{
返回此.brandService.getById(brandsUuid).pipe(
映射(res=>res.logoDesktopUrl)
);
}
下面是一个关于


可能有一种更简单的方法,但它会运行:-)

首先,当您等待时,不应该使用
然后再使用

第二步,
async/await
仅在承诺的情况下运行

async getOpportunitiesByPage(_searchQueryAdvanced: any = 'active:true') {
  const result = await this._opportunities
    .listaOportunidades(this.pageSize, this.currentPage, _searchQueryAdvanced)
    .toPromise();
  this.totalSize = result['totalElements'];
  this.listaOportunidades = result['content'].map(async (opportunities: any) => {
    opportunities.logoDesktopUrl = await this.getBrand(opportunities['brandsUuid']);
    console.log(opportunities.logoDesktopUrl);
    return opportunities;
  });

  this.getTasks(this.totalSize);
}

getBrand(brandsUuid) { 
  return new Promise((resolve, reject) => {
    this.brandService.getById(brandsUuid).subscribe(res => { 
      console.log(res.logoDesktopUrl);
      return resolve(res.logoDesktopUrl);
    }, err => {
      return reject(err);
    });
  });
}
但是,因为
rxjs
是Angular中使用的,所以应该使用它而不是
async/await

getOpportunitiesByPage: void(_searchQueryAdanced: any = 'active:true') {
  this._opportunities.listaOportunidades(this.pageSize, this.currentPage, _searchQueryAdvanced).pipe(
    tap(result => {
      // we do that here because the original result will be "lost" after the next 'flatMap' operation
      this.totalSize = result['totalElements'];
    }),
    // first, we create an array of observables then flatten it with flatMap
    flatMap(result => result['content'].map(opportunities => this.getBrand(opportunities['brandsUuid']).pipe(
        // merge logoDesktopUrl into opportunities object
        map(logoDesktopUrl => ({...opportunities, ...{logoDesktopUrl}}))
      )
    ),
    // then we make each observable of flattened array complete
    mergeAll(),
    // then we wait for each observable to complete and push each result in an array
    toArray()
  ).subscribe(
    opportunitiesWithLogoUrl => { 
      this.listaOportunidades = opportunitiesWithLogoUrl;
      this.getTasks(this.totalSize);
    }, err => console.log(err)
  );
}

getBrand(brandsUuid): Observable<string> {
  return this.brandService.getById(brandsUuid).pipe(
    map(res => res.logoDesktopUrl)
  );
}
getOpportunitiesByPage:void(\u searchQueryDanced:any='active:true'){
此.\u opportunities.ListaOpportUnidades(this.pageSize,this.currentPage,\u searchQueryAdvanced)。管道(
点击(结果=>{
//我们在这里这样做是因为在下一次“flatMap”操作后原始结果将“丢失”
this.totalSize=结果['totalElements'];
}),
//首先,我们创建一个观察数组,然后用flatMap将其展平
flatMap(结果=>result['content'].map(机会=>this.getBrand(机会['brandsUuid']).pipe(
//将logoDesktopUrl合并到opportunities对象中
映射(logoDesktopUrl=>({…机会,{logoDesktopUrl}}))
)
),
//然后,我们使平坦阵列的每个可观测值都是完整的
mergeAll(),
//然后我们等待每个可观察到的结果完成,并将每个结果推送到一个数组中
toArray()
).订阅(
LogoURL=>{
this.listaOportunidades=带有logourl的机会;
this.getTasks(this.totalSize);
},err=>console.log(err)
);
}
getBrand(brandsUuid):可观察{
返回此.brandService.getById(brandsUuid).pipe(
映射(res=>res.logoDesktopUrl)
);
}
下面是一个关于


可能有一种更简单的方法来实现它,但它会运行:-)

是否
this.getBrand()
返回承诺?不,一个简单的服务
getBrand(brandsUuid){this.brandService.getById(brandsUuid).subscribe(res=>{console.log(res.logodektopurl);返回res.logodektopurl;});}
只等待承诺。我建议查看其他可观察的函数来实现这一点,比如可能会返回一个承诺?不,一个简单的服务
getBrand(brandsUuid){this.brandService.getById(brandsUuid).subscribe(res=>{console.log(res.logodektopurl);return res.logoDesktopUrl;});}
仅等待承诺。我建议你看看