2016-10-22 2 views
0

Twilio Authy의 register_user() 콜백 성공 여부가 확실하지 않습니다. 내 코드에서Twilio Authy 콜백 문제

var authyUsrId; 
//global.authyUsrId; 

app.post('/forTwilio', function(req, res){ 
    // send the received data to Twilio Authy 
    authy.register_user('[email protected]', '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에 성공적으로 추가되고 있으며, 응답 상태 내가 register_user()의 성공 콜백에서 뭔가 authyUsrId의 값을 설정하고 그것을 사용하려면 200

이지만 POST 요청에 보내는 JSON 응답

그러나

는 응답에 나는 점점 오전에만이

{name: 'hello'}

특히 register_user() 콜백 부분을 디버깅 할 수있는 방법이 있나요?

답변

1

여기에 Twilio 개발자 전도사가 있습니다.

your answer에서 문제를 해결했음을 알지만, 지금 진행중인 작업과 그 해결 방법에 대해 설명하고 싶습니다. 원래 코드에서

:

app.post('/forTwilio', function(req, res){ 
    authy.register_user('[email protected]', '8753565612', '91', function(err, res){ 
     authyUsrId = 'world'; 
    }); 
    res.json({name: 'hello', msg: authyUsrId}); 
}); 

당신은 Authy에 API 요청에서 콜백 내에서 authyUsrId 변수를 설정하십시오. 그런 다음 호출에 authyUsrId을 사용하여 JSON으로 응답하십시오. 그러나 register_user비동기식 호출이므로 아래 코드는 콜백 내에서 실행되는 코드보다 먼저 실행됩니다. 사실 reguster_user 함수는 HTTP 요청을해야하므로 콜백은 요청이 완료된 후에 만 ​​실행됩니다.

것은이 같은 원래의 코드에 로깅을 추가 한 경우 :

app.post('/forTwilio', function(req, res){ 
    authy.register_user('[email protected]', '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 

귀하의 수정은 당신에게 콜백 내에서 원래의 웹 요청을 응답했다 , 당신이 필요한 모든 데이터를 가지고있을 때. 그것이 작동하는 이유입니다. 원래 코드를 업데이트하는 경우 다음과 같이 표시됩니다.

app.post('/forTwilio', function(req, res){ 
    authy.register_user('[email protected]', '8753565612', '91', function(err, res){ 
     authyUsrId = 'world'; 
     res.json({name: 'hello', msg: authyUsrId}); 
    }); 
}); 

희망적입니다.

+0

감사합니다. @philnash. 정말 도움이되었습니다. – somnathbm

0

해결했습니다. 성공 콜백 register_user()에서 직접 응답을 보내면 작동합니다.

app.post('/forTwilio', function(req, res){ 

    // send the received data to Twilio Authy 
    authy.register_user('[email protected]', '9224753123', '91', function(err, res2){ 
     res.send(res2.user); 
    }); 
});