无法识别 JS 全局变量

JS Global variable not recognized

我将我的变量设置为全局变量,因此 google 将能够读取用户在 google 上的操作中输入的名称。在 "agent.add" 中,我添加了我希望 google 读出的内容。

然而,它只是继续读作 your name is [object Oject] 而不是给定的名字。这是我 index.js 中的代码。

var myName = {}; 

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
    const agent = new WebhookClient({ request, response });

    function naming(agent){
        myName = agent.parameters.givenName;
        agent.add(`your name is ${myName}`);
    }

您可能不应该将其设为全局变量,尤其是因为您只是在本地使用它。

像这样的东西应该可以工作:

function naming(agent){
    let myName = agent.parameters.givenName;
    agent.add(`your name is ${myName}`);
}