Iteration through an array in js

Viewed 70
  var webhook_array = webhook_url.split(",");
  console.log(webhook_array);
  function send(item) {
    console.log(item)
    request.open("POST", item);
    request.setRequestHeader('Content-type', 'application/json');
  
    var myEmbed = {
      title: embed_title,
      color: hexToDecimal(hexcolour),
      description: message_content,
      footer: {
        text: "Powered by Yapplex Tools",
        icon_url: avatarurl,
      }
    }
    
    var params = {
      username: webhook_username,
      avatar_url: avatarurl,
      embeds: [ myEmbed ]
    }
    request.send(JSON.stringify(params));
  }
  webhook_array.forEach(send);

  function hexToDecimal(hex) {
    return parseInt(hex.replace("#",""), 16)
  }
}

This code should go through each webhook in the array and send a message using them. It prints them to console meaning that they are there and that it can detect them, but only one of the webhooks is called(when tested using two webhooks)

1 Answers

There's a few things that could be causing you problems. Most notably, you need to create a new XMLHttpRequest().



  var webhook_array = webhook_url.split(",");
  console.log(webhook_array);
  function send(item) {
    console.log(item)

    /** CREATE REQUEST HERE */
    const request = new XMLHttpRequest();
    /** SHOULD WORK NOW */

    request.open("POST", item);
    request.setRequestHeader('Content-type', 'application/json');
  
    // Other code...
    request.send(JSON.stringify(params));
  }
  webhook_array.forEach(send);

The sample you included also has some undefined variables (avatarurl, webhook_username) and an extra curly brace a the end. Make sure those are in your code elsewhere: they probably just in part of the file that didn't make it into your question. Hope this helps!

Related