我不确定Twilio的register_user()的成功回击是否被解雇了。在我的代码中
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()回调部分?
发布于 2016-10-23 14:44:16
两位开发人员在这里传道。
我看到您已经在your answer中解决了这个问题,但是我只想解释一下发生了什么,以及为什么这是您的解决方案。
在您的原始代码中:
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请求,因此只有在该请求完成后才能运行回调。
如果将日志添加到原始代码中,如下所示:
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});
});您可以在日志中看到:
Sending JSON response
Received response from Authy当您拥有所需的所有数据时,您的修复方法是在回调中响应原始web请求。这就是为什么它能起作用。如果我正在更新您的原始代码,那么现在看起来如下所示:
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});
});
});希望这是有意义的。
发布于 2016-10-22 13:54:42
我解决了。从register_user()的成功回调直接发送响应是有效的。
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);
});
});https://stackoverflow.com/questions/40190752
复制相似问题