วนลูปผ่าน for loop จากนั้นเรียกใช้ฟังก์ชัน JS

Aug 19 2020

ดังนั้นฉันไม่สามารถเลือกที่จะเลือกบุรุษไปรษณีย์มากกว่าวัตถุชิ้นเดียวได้ เมื่อฉัน console.log ฉันจะได้รับวัตถุทั้งหมดของอาร์เรย์ (แบทเทิลแท็กที่นำออกและแทนที่ด้วยผู้เล่น)

        router.get('/cod/', (req, res) => {
        const pdata = {}
        console.log(req.params)
        API.login(process.env.EMAIL, process.env.PASSWORD).then((output) => {
       
        // Players array,
        players = ['player1', 'player2', 'player3']
        // map through array and create promise for each player and store it in an array
        promises = players.map(player => API.MWBattleData(player))
        // Pass all promises to Promise.all
        // Result will be an array of individual output of each promise
        Promise.all(promises)
            .then(result => {
                // Loop through result, and assign the output to pdata
                result.forEach((output, index) => {
                    // index + 1 because starting index will be zero
                    pdata[`p${index + 1}`] = output
                    res.json(pdata);
                })
            })
         })
         });

ฉันได้รับข้อผิดพลาดในโหนดคอนโซล: 19134) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: ไม่สามารถตั้งค่าส่วนหัวหลังจากที่ส่งไปยังไคลเอ็นต์ที่ ServerResponse.setHeader (_http_outgoing.js: 518: 11) ที่ ServerResponse.header (/ Users / jaredschau เดสก์ท็อป / cod-tracker / node_modules / express / lib / response.js: 771: 10) ที่ ServerResponse.send (/Users/jaredschau/Desktop/cod-tracker/node_modules/express/lib/response.js:170:12) ที่ ServerResponse.json (/Users/jaredschau/Desktop/cod-tracker/node_modules/express/lib/response.js:267:15) ที่ /Users/jaredschau/Desktop/cod-tracker/routes/posts.js:29: 25 ที่ Array.forEach () ที่ /Users/jaredschau/Desktop/cod-tracker/routes/posts.js:26:24 ที่ processTicksAndRejections (internal / process / task_queues.js: 97: 5) (node: 19134) UnhandledPromiseRejectionWarning: การปฏิเสธสัญญาที่ไม่มีการจัดการข้อผิดพลาดนี้เกิดขึ้นจากการโยนเข้าไปในฟังก์ชัน async โดยไม่มีบล็อกจับหรือโดยการปฏิเสธสัญญาที่ไม่ได้จัดการด้วย. catch () หากต้องการยุติกระบวนการโหนดในการปฏิเสธสัญญาที่ไม่มีการจัดการให้ใช้แฟล็ก CLI--unhandled-rejections=strict(ดู https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode) (รหัสการปฏิเสธ: 1) (โหนด: 19134) [DEP0018] DeprecationWarning: เลิกใช้งานการปฏิเสธสัญญาที่ไม่สามารถจัดการได้ ในอนาคตสัญญาการปฏิเสธที่ไม่ได้รับการจัดการจะยุติกระบวนการ Node.js ด้วยรหัสออกที่ไม่ใช่ศูนย์

คำตอบ

1 Kumar Aug 19 2020 at 10:05

ดูเหมือนAPI.MWBattleData('Player1')ตอบแทนคำสัญญา จากนั้นคุณสามารถบรรลุเป้าหมายนี้ได้Promise.allซึ่งจะแก้ไขสัญญาทั้งหมดตามลำดับและส่งคืนผลลัพธ์สำหรับคำสัญญาทั้งหมดให้คุณ

// Players array,
players = ['player1', 'player2', 'player3', ...]

// map through array and create promise for each player and store it in an array
promises = players.map(player => API.MWBattleData(player))

// Pass all promises to Promise.allSettled
Promise.allSettled(promises)
  .then(result => { // Result will be an array of individual output of each promise

    // Loop through result, and assign the output to pdata
    result.forEach((output, index) => { 
      // index + 1 because starting index will be zero
      pdata[`p${index + 1}`] = output.value;
    })
  })

แจ้งให้เราทราบหากวิธีนี้เหมาะกับคุณ

elpmid Aug 19 2020 at 10:05

คุณสามารถใช้วิธีการคำมั่นสัญญา () เพื่อสร้างคำสัญญาที่ทำซ้ำได้เพื่อแก้ไขอาร์เรย์ของผลลัพธ์ที่อินพุตสัญญาไว้ ดูเอกสาร: Promise.all ()

ฉันอนุมานได้ว่าคุณมีผู้ใช้มากมาย

const players = [player1, player2, player3];

ในการสร้างอินพุตสัญญาจากพวกเขาคุณสามารถทำได้เช่นนี้โดยใช้เมธอด array.map ที่ส่งคืนอาร์เรย์

Promise.all(players.map(player => API.MWBattleData(player))).then(values => console.log(values))

ค่าที่ส่งคืนคืออาร์เรย์ของผลลัพธ์โดยสัญญาที่สร้างขึ้นจากอาร์เรย์ผู้เล่นของคุณ ฉันหวังว่ามันจะช่วยได้ ไชโย