下面的函数可以打印已经订购的商品和数量。
function confirmitem(agent){
const item = agent.getContext('item'),
Food = item.parameters.Food,
quantity = item.parameters.quantity;
agent.add('Confirming '+Food+' in a quantity of '+quantity);
}
以下是输出上下文
"outputContexts": [{
"name": "projects/simple-dialog-wtckcj/agent/sessions/1a459958-2249-5973-9561-5418940b0b22/contexts/item",
"lifespanCount": 4,
"parameters": {
"Food": "matooke",
"Food.original": "matooke",
"quantity": {
"number.original": "1",
"number": 1
},
"quantity.original": "1"
}
},
{
"name": "projects/simple-dialog-wtckcj/agent/sessions/1a459958-2249-5973-9561-5418940b0b22/contexts/itemconfirm",
"lifespanCount": 4,
"parameters": {
"quantity": {
"number": 1,
"number.original": "1"
},
"quantity.original": "1",
"Food": "matooke",
"Food.original": "matooke"
}
}
]
实际的输出是'确认matooke的数量为[对象对象]'。
问题是 item.parameters.quantity
是一个对象,其值为
{
"number.original": "1",
"number": 1
},
当你试图打印这个对象时,正常的打印方式只是用"[对象对象]",你已经注意到了。
你可能想访问这个对象里面的 "数字 "字段,用类似于 item.parmeters.quantity.number
. 如果你想访问 "原始 "值,也就是用户所说的内容,你可能需要使用类似于 item.parameters["quantity.original"]
. 您需要使用这种索引方法,因为 "quantity.original "包含一个句号。