0

Now when someone calls the twilio number, /hello function in Parse Cloud Code is executed. In /hello function I can find who is making the call by request.param('From'). With this information I want to query that user and then get the Receiver's phone number, which is in the 'Receiver' column of the User Table.

    app.get('/hello', function(request, response) {
    var twiml = new twilio.TwimlResponse('');

// querying the caller
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo("username", request.param('From')); // querying the caller
        query.find( {
                success: function(result) {    
                   twiml.dial({callerId:'+4XXXXXX7'}, result[0].get("Receiver"));  // dialing the receiver
                   console.log("user is " +result[0]);
                   response.success("user is found");
                    },
                error: function (error) {
                        response.error("failed finding the user");
                    }
    });
    response.type('text/xml');
    response.send(twiml.toString(''));
    });
    app.listen();

The querying does not work and the logs that are in the query.find does not print. I tried query.first instead of query.find but that too did not work.

2 Answers 2

1

request.params are the route parameters. In order for the code to pass a From parameter (more properly named from), the route must be defined as follows...

app.get('/hello/:from', function(request, response)

The caller should get /hello/someusername. To use the from parameter in the function...

query.equalTo("username", request.params.from);
// notice the plural: "params"

A little organization advice: to rule out other issues with your code (and to improve the SO question), start out giving this code a single job: to retrieve the user with the passed username:

app.get('/hello/:from', function(request, response) {
    // so we can test getting a user, just get a user
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo("username", request.params.from);
    query.find({
        success: function(result) {
            response.success(result);
        },
        error: function (error) {
            response.error(error);
        }
    });
});

When you get this working, put the Twillio stuff in a function that you call from the success block. This will allow you to debug your code (and ask pointed SO questions) one step at a time.

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

1 Comment

request.param('From') works fine, it gets the caller's number. the only problem is querying the user table. @danh
0

In case someone needs it the following code works.

 app.get('/hello', function(request, response) {
    var twiml = new twilio.TwimlResponse('');

// querying the caller
    Parse.Cloud.useMasterKey();
    var query = new Parse.Query(Parse.User);
    query.equalTo("username", request.param('From')); // querying the caller
        query.find( {
                success: function(result) {    
                   twiml.dial({callerId:'+4XXXXXX7'}, result[0].get("Receiver"));  // dialing the receiver
                   console.log("user is " +result[0]);
                   response.success("user is found");
                   response.type('text/xml');
                   response.send(twiml.toString(''));
                    },
                error: function (error) {
                        response.error("failed finding the user");
                    }
    });
    });
    app.listen();

1 Comment

please, what is the change to make the query and console.log working?

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.