如何将变量从一个函数传递到Node.js中的另一个函数?

时间:2017-03-19 16:09:02

标签: javascript node.js bots luis

我想将topic_to_learn变量传递给第二个函数,并在第二个函数中将其用于其他函数。

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

2 个答案:

答案 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);
    },

答案 1 :(得分: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);
        },