Phillip
Phillip

Reputation: 627

How do I populate array of twilio fax results and send via express?

I am trying to get results from the twilio api like so: twilio => our secured api backend => our client app We are doing this to project our api-keys and other security based purposes.

We have sending faxes down, and also checking for single instances. I am however having a hard time getting the list of faxes we get sent back to our client app after it completes. Mainly due to the fact that it's a repeating call. This is what we have so far for this problem:

app.post('/fax', function (req, res) {
  const faxList = [];
  const getFax = client.fax.faxes.each((faxes) => {
    faxList.push(faxes);
    console.log(faxList);
  });

Right now when I run this I see the array populated one by one just like it should, but can't seem to return the final result after it completes.

From my searches online it looks like I need to utilize Promise.all to send my completed res.status(200).json(faxList); so express can send the list of faxes to our app. I'm having issues setting up the promise.all as the faxList variable is just empty. Almost as if the push done to the array doesn't last once the call completes.

Does this have something to do with the way that twilio has their fax api function set up? https://github.com/twilio/twilio-node/blob/master/lib/rest/fax/v1/fax.js or would this me not understanding how promise.all functions?

I'm newer to the node side of javascript. I have more experience with other languages so I apologize in advance.

Upvotes: 1

Views: 56

Answers (1)

Shachaf.Gortler
Shachaf.Gortler

Reputation: 5735

I would try to get the whole list , if you have less than a page worth of faxes . (I think a page in Twilio is 50 ) like so

return new Promise((resolve) => {
    client.faxes.list().then(function(faxes){
                        if (!empty(faxes)){
                           resolve(faxes);
                        }
                       });
});

Upvotes: 1

Related Questions