首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Twilio Authy回调问题

Twilio Authy回调问题
EN

Stack Overflow用户
提问于 2016-10-22 09:57:27
回答 2查看 187关注 0票数 0

我不确定Twilio的register_user()的成功回击是否被解雇了。在我的代码中

代码语言:javascript
复制
var authyUsrId;
//global.authyUsrId;

app.post('/forTwilio', function(req, res){
    // send the received data to Twilio Authy
    authy.register_user('maverick@example.com', '8753565612', '91', function(err, res){
        //global.authyUsrId = 'world';
                 authyUsrId = 'world';  
    });
    //res.set("Content-Type","application/json");
        res.json({name: 'hello', msg: authyUsrId});
    //res.json({name: 'hello', msg: global.authyUsrId});
});

尽管新用户正在成功地添加到Authy中,并且响应状态为200。

我希望将authyUsrId的值设置为register_user()的成功回调中的某个值,并在发送给POST请求的JSON响应中使用它。

但在回应中我只得到了这个

{name: 'hello'}

是否有任何方法进行调试,特别是register_user()回调部分?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-23 14:44:16

两位开发人员在这里传道。

我看到您已经在your answer中解决了这个问题,但是我只想解释一下发生了什么,以及为什么这是您的解决方案。

在您的原始代码中:

代码语言:javascript
复制
app.post('/forTwilio', function(req, res){
    authy.register_user('maverick@example.com', '8753565612', '91', function(err, res){
        authyUsrId = 'world';  
    });
    res.json({name: 'hello', msg: authyUsrId});
});

将从API请求回调到Authy的authyUsrId变量设置为Authy。然后尝试在调用中使用该authyUsrId来响应JSON。但是,register_user是一个异步调用,因此下面的代码在回调中运行的代码之前运行。实际上,reguster_user函数必须发出HTTP请求,因此只有在该请求完成后才能运行回调。

如果将日志添加到原始代码中,如下所示:

代码语言:javascript
复制
app.post('/forTwilio', function(req, res){
    authy.register_user('maverick@example.com', '8753565612', '91', function(err, res){
        console.log("Received response from Authy");
        authyUsrId = 'world';  
    });
    console.log("Sending JSON response");
    res.json({name: 'hello', msg: authyUsrId});
});

您可以在日志中看到:

代码语言:javascript
复制
Sending JSON response
Received response from Authy

当您拥有所需的所有数据时,您的修复方法是在回调中响应原始web请求。这就是为什么它能起作用。如果我正在更新您的原始代码,那么现在看起来如下所示:

代码语言:javascript
复制
app.post('/forTwilio', function(req, res){
    authy.register_user('maverick@example.com', '8753565612', '91', function(err, res){
        authyUsrId = 'world';  
        res.json({name: 'hello', msg: authyUsrId});
    });
});

希望这是有意义的。

票数 1
EN

Stack Overflow用户

发布于 2016-10-22 13:54:42

我解决了。从register_user()的成功回调直接发送响应是有效的。

代码语言:javascript
复制
app.post('/forTwilio', function(req, res){

    // send the received data to Twilio Authy
    authy.register_user('jimmy@example.com', '9224753123', '91', function(err, res2){
        res.send(res2.user);
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40190752

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档