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

How to pass variable from one function to another function in Node.js?

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

您可能想要使用一个适用于两个函数的超出范围的变量。

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