javascript - 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
解决方案
您遇到了 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"]
应该管用。
推荐阅读
- python - Numpy数组 - 仅在相邻值不重复时获取索引 - python
- ruby-on-rails - 如何使用多个外键制作 Rails 模型 belongs_to
- bash - 如何导入全局可用的变量形式的 bash 脚本?
- sql - 如果今天是星期五,则提取从今天起 7 到 9 天的数据,如果不是 7 天
- sql - 如何在下一个查询中使用一个查询的结果?
- nginx - EC2 Nginx 卡在 SYN_ACK 中,用于来自单个 ip 的 http 请求
- c - 如何识别WindowProcedure中的AppendMenu MF_POPUP Menu?
- javascript - 如何在Javascript中添加参数并删除以前的参数
- mysql - Mysql日期比较中的问题
- ruby - 如何使用 3 个或更多数组中的每个项目创建哈希