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

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

我怎樣才能從異步調(diào)用中獲取數(shù)據(jù)而不是未定義?

我怎樣才能從異步調(diào)用中獲取數(shù)據(jù)而不是未定義?

慕哥6287543 2022-12-22 13:46:11
subjectin user.subjects = subjectsbelowundefined不是一組主題。背景:我有三個(gè)表——用戶、主題以及用戶和主題之間的關(guān)系,稱為 users_subjects。我的目標(biāo)是最終的 res 應(yīng)該包括一組主題名稱。為此,我需要三個(gè)異步調(diào)用。從用戶表中獲取用戶從表示用戶和主題之間關(guān)系的表(users_subjects)中獲取每個(gè)用戶的主題從主題表中獲取每個(gè)主題 ID 的名稱我的挑戰(zhàn)是輸入主題 ID 并接收名稱。addSubjects 的上半部分是我嘗試解決它的嘗試,盡管沒(méi)有成功:   //for every user take its id    //run its id with tutors-subjects to get all subject id with that user id    //for each subject id get is subject name through with subjects service    //add subjects key    //add each subject to an arrayconst addSubject = (knexInstance, users, res) => {  let resultsName = []    function testIt(subjectsArray) {       // let resultsName = []        Promise.all(            subjectsArray.map((id) =>                SubjectsServes.getById(knexInstance, id)                    .then(subjectNameObject => {                       console.log(subjectNameObject)                        resultsName.push(subjectNameObject.subject_name)                    })                    .catch(err => {                        //console.log(err)                    })            )).then(() => {                return resultsName            })    }    let results = []    Promise.all(        users.map((user) =>            TutorsSubjectsService.getAllSubjectsForATutor(knexInstance, user.user_id)            .then(subjectsIdArray => {                    return testIt(subjectsIdArray)                })                .then(sub => {                    user.subjects = sub                    results.push(user)                })                .catch(err => {                    //console.log(err)                })        )).then(() => {            return res.json(results)        })        .catch(err => {            //console.log(err)        })}我遇到的唯一問(wèn)題是 testIt 函數(shù)。到那時(shí)為止一切正常。他們只是在這里查看上下文。我的問(wèn)題是如何從 中獲取正確的 subjectNames testIt?(testItlast中的 returnthen沒(méi)有幫助;整個(gè)函數(shù) testIt 只是undefined如標(biāo)題中所述返回)
查看完整描述

1 回答

?
汪汪一只貓

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

const  testIt = (subjectsArray)=> {

       

        return subjectsArray.map((id) => {

            

               return SubjectsServes.getById(knexInstance, id);

               

            }).reduce((acc,el)=>{

            

                acc.push(el); 

                return acc;

                

               },[])

               

           //return array of promises

        

 }

const addSubject = (knexInstance, users, res) => {

  

   

    

    Promise.all(

        users.reduce((acc,el)=>{ 

           acc.push(TutorsSubjectsService.getAllSubjectsForATutor(knexInstance, el.user_id));              return acc; 

          

          },[])

           

        ).then((userResults) => { 

        //resolve all promises userResult is an array of result from promises above


            Promise.all( userResults.reduce((acc,el)=>{

               //for each user create array of promises given the subject list

               acc = acc.concat(testIt(el))

            } ,[]).then((results)=>{

            

               return res.status(200).send(result);

            }).catch( (err)=>{

            

              console.log(err);

              

              return res.status(500).end();

            } )

        })

        .catch(err => {

              console.log(err);

              

              return res.status(500).end();

        })


}

當(dāng)你 .then() 這意味著你正在解決承諾。所以沒(méi)有返回到 promise.all。 Promise.all().then( result=>{} ) result 是 promises 的輸出數(shù)組。你必須將所有的承諾鏈接在一起。它們是異步的,2 個(gè)承諾都同時(shí)運(yùn)行。所以你的結(jié)果名稱將是不明確的,可能是初始值(空數(shù)組)



查看完整回答
反對(duì) 回復(fù) 2022-12-22
  • 1 回答
  • 0 關(guān)注
  • 81 瀏覽
慕課專欄
更多

添加回答

舉報(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)