Thomas Degroot
Thomas Degroot

Reputation: 524

Updating Twilio MediaUrl for use with Express 4

I am updating my twilio sms messaging tool to use express 4. On one one line of code I am having an issue.

var mediaUrl = request.param('MediaUrl' + i); //deprecated in express 4

When I attempt to update with this line

var mediaUrl = request.query.MediaUrl + i;

it no longer links to the twilio MediaURL. Here is the complete code for reference.

var numMedia = parseInt( request.body.NumMedia );
if (numMedia > 0) {
    for (i = 0; i < numMedia; i++) {

        //var mediaUrl = request.query.MediaUrl + i;
        var mediaUrl = request.param('MediaUrl' + i); //deprecated in express 4

        groupRef.where( {"memberNumber":request.query.From} ).limit(1).on( "value", function ( data ){
            if( data.count() ){
                data.forEach( function( snapshot ){
                    var member = snapshot.value();
                    messagesRef.push({
                        sid: request.body.MessageSid,
                        type:'text',
                        tstamp: new Date().toLocaleString('en-US', { timeZone: "America/Denver" }),
                        fromName:member.memberName,
                        fromNumber:request.body.From,
                        message:"",
                        media:mediaUrl,
                        groupNumber:request.body.To
                    });
                });
            }
        });
    }
}

Upvotes: 1

Views: 58

Answers (1)

Andy
Andy

Reputation: 698

I think it is due to the fact that Twilio is POSTing the data to you and request.query is looking in the querystring. Either change the Twilio method to GET, or use req.body to get the value from the POST.

The simple fix would be to use:

var mediaUrl = request.body.MediaUrl + i;

Upvotes: 2

Related Questions