gpt4 book ai didi

javascript - 无法将 Dialogflow 响应解析为 AppResponse

转载 作者:行者123 更新时间:2023-11-29 18:46:30 24 4
gpt4 key购买 nike

我在 conv.ask 之前执行数组映射时遇到问题。我收到与格式错误的 JSON 对象相关的错误,但据我所知,它应该是有效的。

const timetableCells = timetable.Trains.map((item) => {
return {
cells: [item.Line, item.Destination, item.Car, item.Min],
}
})

conv.ask(new Table({
title: `Rail Timetable for ${station}`,
subtitle: 'Timetable as of x date',
image: new Image({
url: 'http://google.com/image.png',
alt: 'Logo'
}),
columns: [
{
header: 'Line',
align: 'LEADING'
},
{
header: 'Destination',
align: 'LEADING'
},
{
header: 'Car',
align: 'LEADING'
},
{
header: 'Arrival',
align: 'LEADING'
},
],
rows: timetableCells,
buttons: new Button({
title: 'Button Title',
url: 'https://github.com/actions-on-google'
})
}))

这是时间表的样子:

{"Trains":[{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"9"},{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"18"},{"Car":"8","Destination":"Glenmont","DestinationCode":"B11","DestinationName":"Glenmont","Group":"1","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":"36"},{"Car":"8","Destination":"Shady Gr","DestinationCode":"A15","DestinationName":"Shady Grove","Group":"2","Line":"RD","LocationCode":"B09","LocationName":"Forest Glen","Min":""}]}

如果我记录 timetableCells 我有一个看起来像这样的数组,我将它分配给 conv.ask 内的 rows根据 their documentation,这应该等于 Google Assistant 的期望值.

[ { cells: ['1', '2', '3', '4'] }, { cells: ['1', '2', '3', '4'] } ]

如果我将传递给 conv.ask 的 JSON 对象字符串化,它看起来是正确的:

JSON.parse("{\"title\":\"Rail Timetable for Farragut North\",\"subtitle\":\"Timetable as of x date\",\"columns\":[{\"header\":\"Line\",\"align\":\"LEADING\"},{\"header\":\"Destination\",\"align\":\"LEADING\"},{\"header\":\"Car\",\"align\":\"LEADING\"},{\"header\":\"Arrival\",\"align\":\"LEADING\"}],\"rows\":[{\"cells\":[\"RD\",\"Glenmont\",\"8\",\"\"]},{\"cells\":[\"RD\",\"Shady Gr\",\"8\",\"\"]}]}")

我哪里错了?我知道这一行是导致问题的原因,因为我可以替换它并且它可以正常工作。调试器指向 JSON 验证错误,但我似乎看不到它。

最佳答案

我花了很长时间才弄明白,奇怪的是这里没有抛出一个不同的错误。很明显,行中的数据需要与表中的标题数量相匹配,但不明显的是字符串不能为空,否则会出现解析错误。

在某些情况下,我的 API 响应会以空字符串的形式返回某些值,这会导致问题。将代码更改为以下内容可解决问题,因为它确保它们永远不会为空字符串。

const timetableCells = timetable.Trains.map((item) => {
return {
cells: [item.Line || "N/A", item.Destination || "N/A", item.Car || "TBD", item.Min || "TDB"],
}
})

关于javascript - 无法将 Dialogflow 响应解析为 AppResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53462614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com