'My simple "send message" javascript program for Twilio is looping twice, but its only sending a message to the same number, What's missing?

I'm trying to create a simple Javascript program that will send a single message to an array of different numbers. I found a twilio page that got me almost there but it doesn't tell me how to loop. I'm admittedly a programming nubie and found a line of loop code. The program did loop, but it called the same number in the array twice instead of calling the two numbers in the array. Here is the code I was using:

// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const twilio = require('twilio')(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);
const numbers = [+180xxxxxxxx, +180xxxxxxxx,]
const body = 'Hello World'
let nLen = numbers.length
for (let i = 0; i<nLen; i++)
{
twilio.messages
  .create({
        to: numbers,
        from: +180xxxxxxxx,
        body: body
      })
  .then(message => {
        console.log(message.sid);
  })
  .catch(err => console.error(err));
}

Is there another function I need to add in to make it send the message to the second number the second time?



Solution 1:[1]

I've never used twilio but it looks like you give the array of numbers in the .create function instead of one number,

try switching "numbers" to "numbers[i]":

for (let i = 0; i<nLen; i++)
{
twilio.messages
  .create({
        to: numbers[i],
        from: +180xxxxxxxx,
        body: body
      })
  .then(message => {
        console.log(message.sid);
  })
  .catch(err => console.error(err));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Victor Svensson