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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在foreach中調(diào)用mongoose findone并等待請(qǐng)求?

如何在foreach中調(diào)用mongoose findone并等待請(qǐng)求?

泛舟湖上清波郎朗 2021-09-17 17:21:49
我有我使用的項(xiàng)目列表forEach。我檢查每個(gè)項(xiàng)目的類型,對(duì)于一個(gè)項(xiàng)目類型,我需要向 mongoDB 發(fā)送請(qǐng)求,然后等待響應(yīng)以進(jìn)一步使用它。如果這很重要 - 我使用 forEach 在超時(shí)時(shí)使用 socket.io 發(fā)射。這是我現(xiàn)在如何迭代列表:questionList.forEach((question) => {    let respName = "";    let respObj = question;    if (question.type === "some_type") {      // some not important code      // respName = "some_name"     } else if (question.type === "some_typeN") {      // some not important code      // respName = "some_nameN"    } else if (question.type === "some_type4") {      respName = "some_name4"      respObj = getBlockStats(handle, block);    } else {      // some not important code      // respName = "some_nameX"    }    setTimeout(() => {      io.emit(respName, respObj);    }, offset);    offset += offsetIncrease;});這是我在循環(huán)中調(diào)用的函數(shù):const getBlockStats = (handle, block) => {  return GameAnswers.findOne({ handle })    .then(gameAnswers => {      let maxQuesstionInBlock = gameAnswers.answers        .reduce((a, b) => {          return (parseInt(a.ownId) > parseInt(b.ownId) ? a : b)        })      let results = [];      for (let i = 1; i <= maxQuesstionInBlock.ownId; i++) {        let team1AllAnswers = gameAnswers.answers          .filter(el => {            return el.blockId === block          })          .filter((el) => {            return el.team === "team1"          })          .filter((el) => {            return parseInt(el.ownId) === i          });        let team1CorrectAnswers = team1AllAnswers          .filter(el => {            return el.correct === "true"          });        let team2AllAnswers = gameAnswers.answers          .filter(el => {            return el.blockId === block          })          .filter((el) => {            return el.team === "team2"          })          .filter((el) => {            return parseInt(el.ownId) === i          });目前,我對(duì) mongoDB 的請(qǐng)求僅在 forEach 遍歷所有列表之后發(fā)生。因?yàn)閟ome_type4在respObj我有空對(duì)象。我需要在其中得到服務(wù)器的響應(yīng)(即請(qǐng)求停止循環(huán)并等待結(jié)果,然后再進(jìn)一步)
查看完整描述

2 回答

?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

這是我使用 async/await 解決此問題的方法


const questionAction = async (question) => {

    let respName = "";

    let respObj = question;


    if (question.type === "some_type") {

      // some not important code

      // respName = "some_name" 

    } else if (question.type === "some_typeN") {

      // some not important code

      // respName = "some_nameN"

    } else if (question.type === "some_type4") {

      respName = "some_name4"

      respObj = await getBlockStats(handle, block); 

    } else {

      // some not important code

      // respName = "some_nameX"

    }


    setTimeout(() => {

      io.emit(respName, respObj);

    }, offset);


    offset += offsetIncrease;

};


(async () => {

  for (const question of questionList) {

    await questionAction(question);

  }

})()


查看完整回答
反對(duì) 回復(fù) 2021-09-17
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

最好的解決方案是使用async/await來處理異步數(shù)據(jù)庫(kù)調(diào)用和for..of循環(huán)來迭代你的數(shù)組。您不能使用forEach,因?yàn)?async/await 在其中不起作用。

你可以參考這個(gè)來了解 async/await。(重點(diǎn)7.2節(jié))

希望這可以幫助 :)


查看完整回答
反對(duì) 回復(fù) 2021-09-17
  • 2 回答
  • 0 關(guān)注
  • 268 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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