Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Alexa-帮助设置异步意图函数后的外部变量_Javascript_Node.js_Asynchronous_Alexa_Alexa Skills Kit - Fatal编程技术网

Javascript Alexa-帮助设置异步意图函数后的外部变量

Javascript Alexa-帮助设置异步意图函数后的外部变量,javascript,node.js,asynchronous,alexa,alexa-skills-kit,Javascript,Node.js,Asynchronous,Alexa,Alexa Skills Kit,我有一个冒险游戏Alexa skill,它有一些用户输入(在Alexa speak中称为意图)变量,比如playerName。我还有一个节点数组,基本上是游戏的脚本,根据用户的决定带有指向节点的指针。这些消息偶尔会使用playerName之类的变量。一旦提供了playerName,我就很难用playerName更新这些消息。我假设这是我需要做的某种异步回调,但我真的不明白如何才能做到这一点 我的问题是:在我从播放机接收到节点消息后,如何让playerName更新节点消息 var playerNa

我有一个冒险游戏Alexa skill,它有一些用户输入(在Alexa speak中称为意图)变量,比如playerName。我还有一个节点数组,基本上是游戏的脚本,根据用户的决定带有指向节点的指针。这些消息偶尔会使用playerName之类的变量。一旦提供了playerName,我就很难用playerName更新这些消息。我假设这是我需要做的某种异步回调,但我真的不明白如何才能做到这一点

我的问题是:在我从播放机接收到节点消息后,如何让playerName更新节点消息

var playerName = "";
//... other variables here such as asl, etc.

// Questions
var nodes = [{
        "node": 1,
        "message": "What is your name?",
        "next": 2
    },
    {
        "node": 2,
        "message": `Oh, so your name is ${playerName}. That's nice, where are you from?`,
        "next": 3
    },
    //...more nodes here
]
//...much further down
var askNameHandlers = Alexa.CreateStateHandler(states.NAMEMODE, {

    'NameIntent': function() {
        var slot = this.event.request.intent.slots.Name.value; 
        //this is successful! I get the playerName

        playerName = slot; //also successful, see below

        this.handler.state = states.AGEMODE;

        this.emit(':ask', "Hello " + nodes[1].message, playerName);
        //using this for debug
        //playerName is correctly included in the reprompt here, 
        //but not nodes[1].message or via original method below

        helper.yesOrNo(this, 'next');
        //this is where the issues occur. This helper method essentially fetches 
        //the next node that corresponds to the 'next' id value, 3 in this case
    },
    //...more intents, more handlers, etc.
});
因此,我的问题是我从NameIntent中获取值,但当我将其分配给playerName时,它不会显示在消息中,我会得到默认值“”


我已经研究过这一点,这似乎是一个异步问题(可能是回调?),因为很明显我很晚才知道这个名字,但我真的不知道如何实现这一点,因为我对Alexa开发和javascript异步还不熟悉,尽管我不认为需要Alexa知识来解决这个问题。

是的,这是一个问题。只需在回调函数中创建
节点
?还有什么其他的代码在使用它们?@Bergi我在处理程序中有其他的状态,它们有自己的意图集,它们都在查看这些节点数据。我想我可以将它们合并为一个状态和处理程序,但是Alexa可能会听到一个AgeIntent,例如,当我只希望她期望像本例中那样的NameIntent时。这些状态还允许我根据状态以不同的方式实现意图。您还可以在变量中保留全局状态,并只更新它们(每当出现
NameIntent
时推送一个新节点),但是,当代码< >节点< /代码>仍然是空的时候,你必须考虑在其他状态中应该发生什么。@贝尔吉,我不确定我在跟随。若你们说的是推送一个新节点,那个么问题就变成了,若我引用了playerName之类的东西,但若我理解正确的话,我会将它向后分配几步。即便如此,如果我需要另一个意图中的playerName呢?然后我觉得同样的异步问题也会出现。另外,我不知道变量中的全局状态是什么意思。我的意思是,在处理程序中,
push
,其中
playerName
可用。但是是的,如果你在其他方面需要
playerName
,你也必须把它放在你的全局状态中;并考虑在错误的顺序调用步骤时不存在的情况。你总是需要设法处理那个案子。