2017-03-19 1 views
0

topic_to_learn 변수를 두 번째 함수에 전달하고 두 번째 함수에서 다른 변수에 사용하려고합니다.한 함수에서 Node.js의 다른 함수로 변수를 전달하는 방법은 무엇입니까?

function (session, args, next) { 
     var topic_to_learn = builder.EntityRecognizer.findEntity(args.entities, 'topic_to_learn'); 
     builder.Prompts.text(session, 'Sure, can you please also tell me about your goals or anything you want to achieve after learning about this topic?'); 
    }, 
    function (session, results, next) { 
     var learning_goals = results.response; 
     session.send('Got it, let me think...', session.message.text); 
     session.send('Voila! These are the articles related to ' + topic_to_learn, session.message.text); 
    }, 
+3

A [mcve 적어도 전체 코드를 제공하거나 바랍니다. 사이에 쉼표가있는 두 함수 표현식은 혼자 서있을 때 구문 오류입니다. 서로 부르는거야? 그들이 어떻게 사용되는지는 완전히 불분명하다. – Bergi

+0

정말 고마워요. 모든 답변이 저에게 영감을 불어 넣어주었습니다. –

답변

0

두 가지 기능 모두에서 사용할 수있는 위의 범위 변수를 사용할 수 있습니다.

var topic_to_learn; 

    function (session, args, next) { 
     topic_to_learn = builder.EntityRecognizer.findEntity(args.entities, 'topic_to_learn'); 
     builder.Prompts.text(session, 'Sure, can you please also tell me about your goals or anything you want to achieve after learning about this topic?'); 
    }; 

    function (session, results, next) { 
     var learning_goals = results.response; 
     session.send('Got it, let me think...', session.message.text); 
     session.send('Voila! These are the articles related to ' + topic_to_learn, session.message.text); 
    }, 
+0

고마워요 ... 너무 많이 시도했지만 너무 실패했습니다. 두 함수가 다른 함수에 저장되어있는 것처럼 보입니다. –

+0

.matches ('get_learning_plan', [ function (session, args, next) { ... –

+1

가장 좋은 방법은 모든 3 개의 함수에 대한 모듈을 생성하고 후속 함수에서이를 연속적으로 요구하는 것입니다. 이렇게하면 함수 내부의 변수에 객체 속성 (func1)처럼 액세스 할 수 있습니다. topic_to_learn) – Jamie

0
.matches('get_learning_plan', [ 

     function (session, args, next) { 
      var topic_to_learn = builder.EntityRecognizer.findEntity(args.entities, 'topic_to_learn'); 
      builder.Prompts.text(session, 'Sure, can you please also tell me about your goals or anything you want to achieve after learning about this topic?'); 
     }, 
     function (session, results, next) { 
      var learning_goals = results.response; 
      session.send('Got it, let me think...', session.message.text); 
      session.send('Voila! These are the articles related to ' + topic_to_learn, session.message.text); 
     }, 
+0

답변입니까? – evolutionxbox

관련 문제