client.once("ready", () => {
  client.openBox("INBOX", false, (err, box) => {
    if (err) throw err;

    client.search(["UNSEEN"], (err, results) => {
      if (err) {
        console.error("Search error:", err);
        return;
      }

      if (results.length === 0) {
        console.log("No unread emails.");
        client.end();
        return;
      }

      const emailDataArray = []; // Array to store {trxID, amountUSDT}

      const fetch = client.fetch(results, {
        bodies: "",
        struct: true,
      });

      fetch.on("message", (msg, seqno) => {
        let uid;

        msg.on("attributes", (attrs) => {
          uid = attrs.uid;
        });

        msg.on("body", (stream, info) => {
          let buffer = "";

          stream.on("data", (chunk) => {
            buffer += chunk.toString("utf8");
          });

          stream.once("end", () => {
            simpleParser(buffer, (err, parsed) => {
              if (err) {
                console.error("Error parsing email:", err);
                return;
              }

              const from = parsed.from?.value[0]?.address || "";
              const subject = parsed.subject || "";

              // Check if the email matches the criteria
              if (
                from === "no-reply@swaps.app" &&
                subject === "Payment is settled"
              ) {
                console.log(
                  `Matched email from ${from} with subject: ${subject}`
                );

                // Extract the body content (HTML or plain text)
                let body = parsed.html || parsed.text || "";
                if (body) {
                  // Decode quoted-printable if needed
                  if (parsed.html) {
                    body = quotedPrintable.decode(body);
                  }

                  // Convert HTML to plain text for easier regex matching
                  const textBody = htmlToText.convert(body, {
                    wordwrap: 130,
                  });

                  // Extract Transaction ID from the plain text version of the body
                  const transactionIdMatch = textBody.match(
                    /Transaction ID\s*([a-zA-Z0-9-]+)/
                  );
                  const transactionId = transactionIdMatch
                    ? transactionIdMatch[1]
                    : "Transaction ID not found";

                  // Extract Amount in USDT from the email body
                  const amountUSDTMatch = textBody.match(
                    /Amount in USDT\s*([0-9.]+)/
                  );
                  const amountUSDT = amountUSDTMatch
                    ? amountUSDTMatch[1]
                    : "Amount in USDT not found";

                  // Log and store the result in the array
                  console.log(`Transaction ID: ${transactionId}`);
                  console.log(`Amount in USDT: ${amountUSDT}`);

                  // Store the result in the emailDataArray
                  emailDataArray.push({
                    trxID: transactionId,
                    amount: amountUSDT,
                  });
                } else {
                  console.log("No body content available for this email.");
                }
              }
            });
          });
        });

        msg.once("end", () => {
          // Mark email as read if a match is found
          client.addFlags(uid, "\\Seen", (err) => {
            if (err) {
              console.error(`Failed to mark email ${uid} as read:`, err);
            }
          });

          // Log the current state of emailDataArray after each email is processed
          console.log("Current extracted data:", emailDataArray);
        });
      });

      fetch.once("error", (err) => {
        console.error("Fetch error: " + err);
      });

      fetch.once("end", () => {
        console.log("Done fetching emails");
        console.log("All extracted data:", emailDataArray); // Final log after all emails are processed
        client.end();
      });
    });
  });
});

this code should output array like this: [{trxID1,amount1},{trxID2,amount2}] and so on

but the output doesnt have it

PS C:\Users\sarth\OneDrive\Desktop\imap> node app.js
(node:17124) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
(Use `node --trace-warnings ...` to show where the warning was created)
Current extracted data: []
Done fetching emails
All extracted data: []
Matched email from <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS> with subject: Payment is settled
Transaction ID: 4d211f0f-7446-4fc9-8ba0-e40c1bbe4331
Amount in USDT: 24.2