第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在從我的承諾/函數(shù)返回之前等待 forEach 完成

如何在從我的承諾/函數(shù)返回之前等待 forEach 完成

暮色呼如 2023-09-07 17:06:55
我有一個角度函數(shù)來從一個 firestore 集合中獲取產(chǎn)品,然后循環(huán)該查詢的結(jié)果以從另一個集合中查找價格。我怎樣才能等到 forEach 的價格完成后再從外部承諾和外部函數(shù)本身返回?返回的結(jié)果包含一個產(chǎn)品數(shù)組,但每個產(chǎn)品的價格數(shù)組為空。 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;      });  }我也嘗試過這種方法,但不知道如何訪問結(jié)果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 回答

?
函數(shù)式編程

TA貢獻1807條經(jīng)驗 獲得超9個贊

根據(jù)評論發(fā)布為社區(qū) Wiki 答案。

對于這種情況,使用 aforEach()不是正確的選擇。正如此處所澄清的那樣,無法forEach()await函數(shù)正常工作,這樣,就無法與您的承諾正常工作。考慮到這一點以及您想要按順序讀取數(shù)據(jù)的事實 - 因為一個查詢的結(jié)果將影響第二個查詢 - 您需要使用普通 , 來循環(huán)for數(shù)據(jù)和數(shù)組。


查看完整回答
反對 回復(fù) 2023-09-07
?
不負(fù)相思意

TA貢獻1777條經(jīng)驗 獲得超10個贊

讓 getProducts() 函數(shù)成為一個承諾。因此,只有當(dāng)您解決它(或拒絕它)時它才會返回。


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 來調(diào)用promise


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

 const products = res;

})

使用等待


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


查看完整回答
反對 回復(fù) 2023-09-07
?
守著一只汪

TA貢獻1872條經(jīng)驗 獲得超4個贊

此版本的代碼有效:


  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);

          };

        });

    })

  }


查看完整回答
反對 回復(fù) 2023-09-07
  • 3 回答
  • 0 關(guān)注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號