herokuparse-platformtimeoutparse-cloud-codebefore-save

Parse Cloud Code beforeSave timeout


When adding an email address to a list I'd like to check in a beforeSave function in my cloud code if the address belong to an existing user. If it doesn't I'd like to stop the save call and return an error response to my mobile app.

When I run the below code I have no problem while entering a valid email address. As soon as I enter an invalid address however the beforeSave function goes into a tizzy and times out after some time, returning a load of rubbish to the the client.

Parse.Cloud.beforeSave("EventUsers", function(request, response) {
    var email = request.object.get("email");
    console.log("starting beforeSave for user: " + email);

    Parse.Cloud.useMasterKey();
    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo("email", email);

    userQuery.first().then(function(user) {
        console.log("user: " + user.get("email"));
        if (user) {
            console.log("User exists");
            response.success();
        }
        console.error("No user with that email");
        response.error("199");
    }, function(error) {
        console.error(error);
        response.error("198");
    });
});

When I run this with an invalid email address I only get the very first console.log calls reported to my console - none of the others are showing.

I'm running my parse server on Heroku.


Solution

  • Are you running this on parse.com or on your own mongo db backend?

    In any event, your problem is that email is likely not indexed so it is doing a full table scan. If it is your own backend, you can put an index on email in the collection.

    If you're running your own db, not sure if anyone has done it yet (I haven't yet, but should), but you can probably also just put a unique constraint on email and then you can simply catch the rejected promise on user.save() and not worry about the beforeSave hook at all.