2
  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)

6
  • you mean your items are being printed in "console.log(item)" but the http requests are not being made? Commented Jun 21, 2020 at 21:33
  • Yes, exactly... Commented Jun 21, 2020 at 21:34
  • 1
    You need to use async/await and wait for each http operation to complete before moving on to the next one. Usually with async await patterns it's not a good idea to use forEach. Instead, wrap the entire code in an anonymous async function, and to loop through each web_hook with a for loop using await on each function call. Also, assuming request is an XHR object, you may consider using fetch API instead, since you can use await on them. If you are having difficulty in understanding, I can write the code as well. Commented Jun 21, 2020 at 21:41
  • @lanxion Why do I need to wait for one request to finish before moving forward? I can certainly send multiple requests concurrently. Commented Jun 21, 2020 at 21:42
  • Have you checked if you have an exception in the JSON.stringify(params) call? I noticed that there's a superfluous comma in the line where you use the avatarurl. Maybe that causes problems with the serialization and that causes request.send() to fail? Commented Jun 21, 2020 at 21:43

1 Answer 1

3

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!

Sign up to request clarification or add additional context in comments.

1 Comment

The undefined variables are because this is not the full code, only enough to replicate my problem. Those variables do have values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.