how to create custom object/array using twilio fetch conference function in Node.js
NickName:Aamer Rasheed Ask DateTime:2018-11-12T15:17:44

how to create custom object/array using twilio fetch conference function in Node.js

I have been trying to generate custom object array or JSON object to fetch specific fields of conferences object that Twilio fetch conference API provides. But since Node.js is async so unable to achieve that and gets undefined in return as their is call back in each function. Tried various approaches but unable to get the result as due to operation nature of node.js call back is registered and next statement is executed. Tried various options of promises and async-await as well. However, unable to achieve a result which might be a call back within each loop (my guess).

Since new to Node.Js so need some suggestion how to achieve. One option is to use settimeout but again this is not a genuine solution as in case of a system in production unable to get information on how much time required. Code is given below:

exports.fetchLiveConferences = function(req, res, next) {
    let conferencesArray = [];

    client.conferences.each({
        dateCreated: new Date(),
        status: 'In-progress'
    }, conferences => {
        /* call back function at this point conferences object is available and can extract properties so pushing it to my array.
         */
        conferencesArray.push({
            conferenceSid: conferences.sid,
            conferenceName: conferences.friendlyName,
            conferenceStatus: conferences.status,
        });
    })

    console.log(conferencesArray);
    /* due to async nature the below statement is executed once call back is registered by node.js in event loop and undefined is returned. 
     */
    res.send(conferencesArray);
}

Copyright Notice:Content Author:「Aamer Rasheed」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53257435/how-to-create-custom-object-array-using-twilio-fetch-conference-function-in-node

Answers
philnash 2018-11-13T02:38:59

Twilio developer evangelist here.\n\nYour issue here is that you are using each to list the conferences independently. If you just want to work with the list, then you should use the list method and then return the results in the callback.\n\nTry something like this.\n\nexports.fetchLiveConferences = function(req, res, next) {\n client.conferences.list({\n dateCreated: new Date(),\n status: 'In-progress'\n }, (err, conferences) => {\n if (err) { console.error(err); return; }\n conferencesArray = conferences.map(conference => ({\n conferenceSid: conference.sid,\n conferenceName: conference.friendlyName,\n conferenceStatus: conference.status,\n }));\n res.send(conferencesArray);\n })\n}\n\n\nIn this case the conferences onject in the callback is a list of conferences. You can then map over the conferences and build the objects that you want, returning them as the response.\n\nUpdate\n\nIf you want to uses promises and async and await the list method also returns a promise. You can use it like this.\n\nconst fetchConferenceList = () => {\n return client.conferences.list({\n dateCreated: new Date(),\n status: 'In-progress'\n }).then(conferences => conferences.map(conference => ({\n conferenceSid: conference.sid,\n conferenceName: conference.friendlyName,\n conferenceStatus: conference.status,\n })));\n}\n\nexports.fetchLiveConferences = async function(req, res, next) {\n const conferenceData = await fetchConferenceList();\n res.send(conferenceData);\n}\n",


R.A. Lucas 2018-11-12T08:14:53

You should just be able to put your res.send function inside the callback like such:\n\nclient.conferences.each({\n dateCreated: new Date(),\n status: 'In-progress'\n}, conferences => {\n /* call back function at this point conferences object is available and can extract properties so pushing it to my array.\n */\n conferencesArray = conferences.map(c => {\n return {\n conferenceSid: c.sid,\n conferenceName: c.friendlyName,\n conferenceStatus: c.status,\n }\n });\n res.send(conferencesArray);\n})\n",


More about “how to create custom object/array using twilio fetch conference function in Node.js” related questions

how to create custom object/array using twilio fetch conference function in Node.js

I have been trying to generate custom object array or JSON object to fetch specific fields of conferences object that Twilio fetch conference API provides. But since Node.js is async so unable to a...

Show Detail

Twilio conference Room ...how to get conference room name?

I was looking for some good resource for setting up a conference call feature using Twilio and came across this link. https://www.twilio.com/docs/howto/moderated-conference This link is a great

Show Detail

Add People in conference using Twilio

I having a problem in Twilio conferences. Is it possible to add a participant in conference while the agent is in the same conference? I tried to hold the Participants and make a call. It was succe...

Show Detail

Create Twilio Conference With Rest API

I'm looking to create a Twilio Conference through the rest API. And I'm not sure how to start the conference. I'd prefer to do this without the SDK. Heres the flow that I'm looking for. In browse...

Show Detail

How to get Conference Sid at the time of dialing twilio call

I've been working with twilio, using Node.js, and dialing call between two web end points. One is client and other is agent. I'm using following code to dial call. function dialCall(calledNumb...

Show Detail

Twilio Conference - Rejoin after hanguponstart

We have a Twilio conference setup in place, and the main user (moderator) is able to do some management of the conference from a conference menu. To access this, they press *. This is set when the

Show Detail

Twilio conference call with nodejs

I want to develop a conference application using Twilio and Nodejs. In my application first person get a call from twilio number after that first person who received the call need to create a confe...

Show Detail

How to Create Twilio Conference Before Calling Anyone?

I'm looking for a way to generate a Conference resource within the Twilio system before adding anyone to that conference. The official recommended way to start a Conference is by returning TwilXML in

Show Detail

using twilio account how to create a dynamic conference call for voiceurl in php

I am trying to upload using client on twilio and trying buy a twilio number. Also, I need to get a dynamic voice URL in order to record a conference call dynamically. <?php require_once 'vendor/

Show Detail

Twilio voice conference time out

We are using Twilio's conference call for creating calls between different parties. https://www.twilio.com/docs/voice/tutorials/how-to-create-conference-calls Is there a way in which, if, partici...

Show Detail