Parse.Com-云代码中的HTTP方法,如何等待响应 [英] Parse.Com - HTTP method in cloud code, how do I wait for the response

查看:54
本文介绍了Parse.Com-云代码中的HTTP方法,如何等待响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的解析云代码中,beforeSave中的HttpRequest已成功执行,但是代码在我没有时间解析响应并确定是否要返回response.success()response.error()之前就炸断了.

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

推荐答案

我可以使用它... Parse.Cloud.httpRequest()是异步的,这是对我有用的解决方案,希望对其他人有所帮助.

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

这篇关于Parse.Com-云代码中的HTTP方法,如何等待响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆