首页 > 解决方案 > Dialogflow system.entity location: location.admin-area is not defined in online editor

问题描述

I use system entity @sys.location in an intent in a Dialogflow agent. In the fulfillment section, I have this function in online code editor:

function testLocation(agent) {
    //check object location
    console.log(' location is ' +  JSON.stringify(agent.parameters.location));
    if(agent.parameters.location.city) {

        //do smthing 
    }
    else if (agent.parameters.location.admin-area){
      agent.add(`this is not recognized ` +agent.parameters.location.admin-area); 
    }else{
     //....
    }
  }

Point is that I receive a warning sign in the editor saying 'area is not defined', but I can see its values from the Firebase Console :

{"country":"","city":"","admin-area":"Piemonte","business-name":"","street-address":"","zip-code":"","shortcut":"","island":"","subadmin-area":""}

Any clues? Thanks in advance

标签: javascriptdialogflow-es

解决方案


您遇到了 JavaScript 语法问题。

表达方式

agent.parameters.location.admin-area

被评估为

agent.parameters.location.admin - area

也就是说,这会导致agent.parameters.location.admin错误area,因为正如错误所说,“位置”的“区域”属性没有定义。

在此,agent.parameters.location是一个对象,JavaScript 提供了两种访问对象属性的方法

  • 您可以使用括号表示法[expression],其中括号内的表达式应计算为对象属性的名称。通常这需要是一个字符串。
  • 在某些情况下,您可以使用点表示法.name,其中名称是属性的名称。但这假设名称没有其他 JavaScript 语法使用的字符。

注意“表达式”和“名称”之间的区别。第一个允许您使用其中包含字符串的变量,或者您已经计算的其他内容。第二个要求您对其进行硬编码。

在您的情况下,您可以使用括号表示法来获得您想要的值。所以像

agent.parameters.location["admin-area"]

应该管用。


推荐阅读