为什么var不设置全局变量?

时间:2019-10-03 20:46:39

标签: node.js firebase

我有一个大型程序,可以很好地在Dialogflow上与GA家用扬声器进行对话,但是增加对html的支持使测试情况表明,通过两个函数别名进行对话是一个变量。一个很小的最小示例轻松显示了它,我想对其进行修复,并知道为什么坏事也是如此。

因此,将其添加,设置为零,然后查看每个变量的维护值,不幸的是两个值。好像有一个     var HtmlCounter = 0;     var SpeakerCounter = 0;

很显然,欲望只有一个价值,而没有两个。它是别名,“或其他”!

我尝试过: ()制作var globals = {},然后附加别名变量。 ()更改变量使用的嵌套,以使Dialogflow感觉到它更多地拥有该变量。

'use strict';


// Me Daniel B Kolis
// dankolis@gmail.com

var versionNow = "03 Oct 2019 16:00";

// This is the troublesome variable
var aCounter = -1;

const functions =               require( 'firebase-functions' );
const express =                 require( 'express' );
const eapp = express();

const {WebhookClient} =         require( 'dialogflow-fulfillment' );
var bodyParser     =            require( 'body-parser' );

// Middleware
const cors =                    require( 'cors' ) ( { origin: true } );
eapp.use( cors );


// Import the service function and various response classes
const { dialogflow, actionssdk, Image, Table,   Carousel } = 
      require( 'actions-on-google' );

// Dialog flow talks to humans using google assistant
const app = dialogflow();

// Three http pages via the express package
eapp.get( '/show_counter.html', (req, res) => 
{
    let moreSeeable = "Counter is " + aCounter.toString();
    res.send( moreSeeable );
});

eapp.get( '/reset_counter.html', (req, res) => 
{
    let moreSeeable = "Acounter is this now: " + aCounter.toString() + 
                      " but right now, set to 0";
    aCounter = 0;
    res.send( moreSeeable );
});

eapp.get( '/add_counter.html', (req, res) => 
{
    let moreSeeable = "Acounter is this now: " + aCounter.toString() + 
                      " but lets add one to it";
    res.send( moreSeeable );
    aCounter = aCounter + 1;
});


// Here we are configuring express to use body-parser as middleware
eapp.use( bodyParser.urlencoded( { extended: false } ) );
eapp.use( bodyParser.json() );

// Note: This `api` must match with `/firebase.json` rewrites rule
exports.api = functions.https.onRequest( eapp );

// Start up hint if you look at logs locally or on the cloud
console.log( "VR: " + versionNow );
console.log( "First line(s) in program, sort of. Now what ?" );
console.log( "aCounter starts at: " + aCounter );

// Create I/O object for dialogs
exports.dialogflowFirebaseFulfillment = 
functions.https.onRequest((request, response) => 
{
    const agent = new WebhookClient({ request, response });

    // Left part is triggered by human speech, right is the
    // function it calls
    let intentMap = new Map();
    intentMap.set( 'addone', addOne );
    intentMap.set( 'questioncounter', questionCounter );
    intentMap.set( 'resetcounter', resetCounter );
    agent.handleRequest( intentMap );
});


// Three functions called by intents, a decode of speech spoken
function questionCounter ( agent ) 
{
    // Computers speak this
    agent.add( 'Counter is ' + aCounter.toString() );
}

function addOne( agent ) 
{
    let rightNow = aCounter;
    aCounter = aCounter + 1;
    agent.add( 'Counter is now ' + rightNow.toString() + 
               ' will be: ' + aCounter.toString() );
}

function resetCounter( agent ) 
{
    aCounter = 0;
    agent.add( 'Counter set to zero' );
}

// End of sad submission; last line of code

目标:通过Dialogflow进行的语音访问和语音访问之间的HTML修改和语音修改都是一个值aCounter,而不是两个。并希望完全理解它!

非常感谢您的关注, 丹·科利斯

1 个答案:

答案 0 :(得分:0)

我很惊讶没有人跳进去。无论如何,答案可能会帮助其他人(也许您呢?)

WWW调用和Dialogflow调用在云处理中生成整个EXE的不同副本。一直到\ tmp mini文件系统...

处理范围并不十分清楚DF人和WWW人是否相同,无论如何构建,这都是不切实际的。

因此,如果您认为委派的FB程序正在执行不应动态执行的操作,请停止并考虑是否对不喜欢的特定实例进行了1个以上的复制。

相关问题