Search code examples
javascriptparse-platformwebhookspfquery

Parse custom webhook: can I query my tables?


In a Parse custom webhook, which is of the form:

app.post('/receiveSMS', function(req, res) {

Where receiveSMS is hooked up to the Twilio api and this method is properly called (I have logs to prove it), but I'm trying to query on my tables within this method and it doesn't seem to be working.

Is this allowed, or is there anything special I need to do to make this work?

var contactObj = Parse.Object.extend("Contact");
var contactQuery = new Parse.Query(contactObj);
console.log(req.body.From);

contactQuery.each(function(contact) {

and the body of the each call never gets called. Is this allowed, and if so, what am I doing wrong here?

Update -- The entirety of the webhook code block is:

app.post('/receiveSMS', function(req, res) {
console.log('receive SMS');
console.log(req.body.Body);

res.send('Success');
if(req.body.Body.toLowerCase() == "in" || req.body.Body.toLowerCase() == "out") {
    twilio.sendSMS({
        From: "(xxx) xxx-xxxx",
        To: req.body.From,
        Body: "It's been noted, and notifications have been sent. Check us out!"
    }, {
        success: function(httpResponse) {
            console.log(httpResponse);
            response.success("SMS Sent!");
        }, 
        error: function(httpResponse) {
            console.error(httpResponse);
            response.error("Uh OH, something went wrong");
        }
    });

    if(req.body.Body.toLowerCase() == "in") {
        console.log("in was received");
        // eventQuery
        var contactObj = Parse.Object.extend("Contact");
        var contactQuery = new Parse.Query(contactObj);
        console.log(req.body.From);

        // contactQuery.equalTo("phone", req.body.From);
        contactQuery.first({
            success: function(contact) {
                console.log("found contact");
                console.log(contact);
            }, error: function(error) {
                alert("Error: " + error.code + " " + error.message);
            }
        });
    }
}
});

This code is called and the logs "console.log('receive SMS')" and the like are all called, except for what is inside the query's first call.


Solution

  • Queries on tables is fine, but you can't use the each() function, as that is restricted to only work in background jobs.

    You'll have to use find() or first() or get() depending on your needs.

    UPDATE

    OK, after seeing your full code I have some ideas as to why it isn't working. First off you're sending res.send("Success"); before you're finished, I'm not positive but I think this causes it to stop running the rest of your code (haven't checked, could be wrong).

    Also you're doing multiple async operations without chaining them so the contactQuery.first() will run before the twilio.sendSMS() is finished.

    Inside twilio.sendSMS() you're calling response.success() / response.error(). These are for cloud methods, not web hooks, so I expect these would be throwing errors server-side (check the logs on the Dashboard).

    Inside contactQuery.first() you are using alert() which isn't supported in cloud code.

    I'm not sure if those mistakes will be caught early and throw errors or if they'll raise run-time exceptions, but they should be fixed, your code re-deployed and try again. Then report any errors in the server logs.