为了账号安全,请及时绑定邮箱和手机立即绑定

如何在从我的承诺/函数返回之前等待 forEach 完成

如何在从我的承诺/函数返回之前等待 forEach 完成

暮色呼如 2023-09-07 17:06:55
我有一个角度函数来从一个 firestore 集合中获取产品,然后循环该查询的结果以从另一个集合中查找价格。我怎样才能等到 forEach 的价格完成后再从外部承诺和外部函数本身返回?返回的结果包含一个产品数组,但每个产品的价格数组为空。 const products = await this.billingService.getProducts();async getProducts() {    let result = [];    let product = {};    return this.db.collection(      'products',      ref => { ref        let query: Query = ref;          return query.where('active', '==', true)      })      .ref      .get()      .then(function (querySnapshot) {        querySnapshot.forEach(async function (doc) {          product = doc.data();          product['prices'] = [];          await doc.ref            .collection('prices')            .orderBy('unit_amount')            .get()            .then(function (docs) {              // Prices dropdown              docs.forEach(function (doc) {                const priceId = doc.id;                const priceData = doc.data();                product['prices'].push(priceData);              });            });        });        result.push(product);        return result;      });  }我也尝试过这种方法,但不知道如何访问结果await this.billingService.getProducts().then(results =>getProducts() {      const dbRef =        this.db.collection(          'products',          ref => { ref            let query: Query = ref; return query.where('active', '==', true)        });       const dbPromise = dbRef.ref.get();      return dbPromise        .then(function(querySnapshot) {          let results = [];          let product = {};          querySnapshot.forEach(function(doc) {            let docRef = doc.ref              .collection('prices')              .orderBy('unit_amount')              results.push(docRef.get())          });          return Promise.all(results)        })        .catch(function(error) {            console.log("Error getting documents: ", error);        });    } 
查看完整描述

3 回答

?
函数式编程

TA贡献1807条经验 获得超9个赞

根据评论发布为社区 Wiki 答案。

对于这种情况,使用 aforEach()不是正确的选择。正如此处所澄清的那样无法forEach()await函数正常工作,这样,就无法与您的承诺正常工作。考虑到这一点以及您想要按顺序读取数据的事实 - 因为一个查询的结果将影响第二个查询 - 您需要使用普通 , 来循环for数据和数组。


查看完整回答
反对 回复 2023-09-07
?
不负相思意

TA贡献1777条经验 获得超10个赞

让 getProducts() 函数成为一个承诺。因此,只有当您解决它(或拒绝它)时它才会返回。


getProducts() {

return new Promise((resolve,reject)=> {

let result = [];

let product = {};

this.db.collection(

  'products',

  ref => { ref

    let query: Query = ref;

      return query.where('active', '==', true)

  })

  .ref

  .get()

  .then(function (querySnapshot) {

    querySnapshot.forEach(async function (doc) {

      product = doc.data();

      product['prices'] = [];


      doc.ref

        .collection('prices')

        .orderBy('unit_amount')

        .get()

        .then(function (docs) {

          // Prices dropdown

          docs.forEach(function (doc) {

            const priceId = doc.id;

            const priceData = doc.data();

            product['prices'].push(priceData);

          });

          resolve(result);// returns when it reaches here

        });

    });

    result.push(product);

  });

 })

}

然后你可以使用 then 或await 来调用promise


this.billingService.getProducts().then( res => {

 const products = res;

})

使用等待


const products = await this.billingService.getProducts();


查看完整回答
反对 回复 2023-09-07
?
守着一只汪

TA贡献1872条经验 获得超3个赞

此版本的代码有效:


  getProducts(): Promise<any> {

    return new Promise((resolve,reject)=> {

      let result = [];

      let product = {};

      this.db.collection(

        'products',

        ref => { ref

          let query: Query = ref;

          return query.where('active', '==', true)

        })

        .ref

        .get()

        .then(async function (querySnapshot:firebase.firestore.QuerySnapshot) {

          for(const doc of querySnapshot.docs) {

            const priceSnap = await doc.ref

              .collection('prices')

              .orderBy('unit_amount')

              .get()

            product = doc.data();

            product['prices'] = [];

            // Prices dropdown

            for(const doc of priceSnap.docs) {

              const priceId = doc.id;

              let priceData = doc.data();

              priceData['price_id'] = priceId;

              product['prices'].push(priceData);

              resolve(result);// returns when it reaches here

            };

            result.push(product);

          };

        });

    })

  }


查看完整回答
反对 回复 2023-09-07
  • 3 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信