1

In my parse cloud code, the HttpRequest in beforeSave is getting executed successfully but the code blows through before I have had time to parse the response and determine whether I want to return a response.success() or a response.error().

I know I am missing something here, any input, ideas from the community here would be appreciated. Thanks

Parse.Cloud.beforeSave(Parse.User, function (request, response) {    
    var user = request.object;
    var key = user.get("recaptcha");  

        Parse.Cloud.httpRequest({
        url: 'https://www.google.com/recaptcha/api/siteverify?secret=<ITS A SECRET>&response=' + key,
        success: function (httpResponse) {
            var status = JSON.parse(httpResponse.text).success;
            console.log(status);
            if (status === false) {
                response.error();
            } else {
                response.success();
            }
        }
    });
});
13
  • So you want to process the JSON content? Commented Nov 5, 2015 at 22:35
  • Hi there, thanks for getting in touch, I am just looking to extract the status value from the json response, and if the status is true then save the data (via response.success()) into the user table else do not save the data. (response.error()) Commented Nov 5, 2015 at 22:51
  • So you're getting the status out? Or do you need to change to ['success'] to index into the JSON object Commented Nov 5, 2015 at 22:54
  • That is correct, I am getting status from the json response, problem is that it blows through before the response has been returned from Google recaptcha. Commented Nov 5, 2015 at 23:02
  • I mean, do you see the status printed in the log? The only other thing to do is use a promise instead of the simple callback. Are you sure the http request doesn't return an error you aren't handling? Commented Nov 5, 2015 at 23:23

1 Answer 1

3

I got it working...Parse.Cloud.httpRequest() is asynchronous, here is the solution that worked for me, hope it helps someone else.

Parse.Cloud.beforeSave(Parse.User, function (request, response) {    
    var user = request.object;
    var key = user.get("recaptcha");  
    if (!request.object.existed()) {
        return Parse.Cloud.httpRequest({
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },                
            url: 'https://www.google.com/recaptcha/api/siteverify?secret=<ITS A SECRET>&response=' + key,
            body: request,
            success: function(httpResponse) {
                var status = JSON.parse(httpResponse.text).success;
                if (status === false) {
                    response.error();
                } else {
                    response.success();
                }
            },
            error: function(httpResponse) {
                response.error(httpResponse);
            }  
        });
        }
       });
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use Parse Promises.

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.