EsmaGo
EsmaGo

Reputation: 161

Parse.com querying user data does not work

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.

Upvotes: 0

Views: 398

Answers (2)

EsmaGo
EsmaGo

Reputation: 161

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();

Upvotes: 0

danh
danh

Reputation: 62686

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.

Upvotes: 1

Related Questions