firebase 异步函数调用未向父调用函数生成任何结果

hof1towb  于 2023-01-18  发布在  其他
关注(0)|答案(2)|浏览(136)

我试图检索给定documentID的文档字段值。我已经解决了db调用部分,但无法将数据发送回去。就好像下面的函数this.userDbService.getVendorNameFromUserId根本没有被调用一样。
下面是父函数:

async getVendorName() {
  let name;

  //blindtiger not going here.

  //calling to 'private userDbService: UserDbService'
   name = await this.userDbService.getVendorNameFromUserId('2OD1yTnMrecMgqljvHFHDjug1VT2');
   console.log(name);
   return name.toString();}

下面是函数getVendorNameFromUserId(''):

async getVendorNameFromUserId(id: string): Promise<string> {
    let vendorName = '';
    const userCollectionRef = await doc(this.store, 'clients/' + id);

    await docData(userCollectionRef, {idField: 'id'}).forEach(el => {

      //correctly is 'blindtiger' from db call.
      console.log(el['user'].companyName.toString())
      return el['user'].companyName.toString();
    });
    //parent function doesn't yield any results - as if this is not called.
    // It should yield 'blindtiger' here.
    return 'NOT FOUND';
  }

getVendorName()是在一个类型脚本文件的构造函数中调用的,这个函数结果的值将用于获取该供应商名称的所有产品。

constructor(private shopifyService: ShopifyService, private userDbService: UserDbService, private modalService: NgbModal, private userAuthService: UserAuthService, private itemDb: ItemDbService) {
    let vendorName: string;

    const name = this.getVendorName();
    console.log(name);
    this.getAllProducts(name).then(result => {
      console.log(result);
      this.itemsToSell = result;
    });
  }`

我有一种感觉,我错过了一些与异步相关的东西-- buy会喜欢任何关于为什么“blindtiger”的价值甚至没有被传递到链上的建议。
我尝试过在函数调用- getVendorNameFromUserId()中添加异步等待,如果我在docData()的from中删除了等待关键字:

await docData(userCollectionRef, {idField: 'id'}).forEach(el => {

      //correctly is 'blindtiger' from db call.
      console.log(el['user'].companyName.toString())
      return el['user'].companyName.toString();
    });`

我得到的结果是'NOT FOUND' --这意味着它正确地将数据向上传递。我认为调用函数没有等待这个函数是有问题的,看起来像这样:

async getVendorName() {
    let name;

    //blindtiger not going here.

    //calling to 'private userDbService: UserDbService'
    name = await this.userDbService.getVendorNameFromUserId('2OD1yTnMrecMgqljvHFHDjug1VT2');
    console.log(name);
    return name.toString();
  }

docData返回一个可观察对象。

const data = await docData(userCollectionRef, {idField: 'id'}).pipe(take(1)).subscribe(user => {
  vendorName = user['user'].companyName;

  //does not yield blind tiger
  console.log({dataDone: vendorName});
})

//does yield blind tiger
console.log({dataAfter: vendorName})
return vendorName['user'].companyName

}

pw9qyyiw

pw9qyyiw1#

我假设docData()返回一个数组,其中只需要第一个值,因为method-signature读取getVendorNameFromUserId,而您只传递了一个id,因此您的代码如下所示:

async getVendorName() {
    const name = await this.userDbService.getVendorNameFromUserId('2OD1yTnMrecMgqljvHFHDjug1VT2');
    console.log('name', name);
    return name;
}

async getVendorNameFromUserId(id: string): Promise<string> {
    const userCollectionRef = await doc(this.store, 'clients/' + id);
    console.log('userCollectionRef', userCollectionRef);

    const resultArray = await firstValueFrom(docData(userCollectionRef, {idField: 'id'}));
    console.log('resultArray', resultArray);

    // Get the first element from array, given the array has at least one element
    const vendorName = resultArray?.map(el => el.user.companyName.toString())?.[0];
    return vendorName ?? 'NOT FOUND';
}

请注意:您不需要在getVendorName()中写入toString(),因为getVendorNameFromUserId()已经返回了string类型的承诺。

pn9klfpd

pn9klfpd2#

你的前途是有回报的:它将返回foreach的结果,而不是getVendorNameFromUserId的结果。请改用常规forof

async getVendorNameFromUserId(id: string): Promise<string> {
  let vendorName = '';
  const userCollectionRef = await doc(this.store, 'clients/' + id);

  const data = await docData(userCollectionRef, {idField: 'id'});
  for (const el of data) { 
    //it will return the first found. Don't you need condition to search from criterias?
    //correctly is 'blindtiger' from db call.
    console.log(el['user'].companyName.toString())
    return el['user'].companyName.toString();
  }
  //parent function doesn't yield any results - as if this is not called.
  // It should yield 'blindtiger' here.
  return 'NOT FOUND';
}
  • 优点:避免构造函数中的异步,对于异步初始化,可以在Angular 中使用APP_INITIALIZER。

相关问题