在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'
})
}))
timetable
如下所示:
{"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
,则有一个看起来像这样的数组,然后将其分配给rows
内的conv.ask
,该数组应该等于Google Assistant期望的值到their documentation。
[ { 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验证错误,但我似乎看不到它。
答案 0 :(得分:3)
我花了很长时间才弄清楚,很奇怪没有在这里抛出其他错误。显然,行中的数据需要与表中的标头数量匹配,但是并不清楚字符串不能为空,否则会出现解析错误。
在某些情况下,某些值从我的API响应中返回为空字符串,这导致了问题。将代码更改为以下代码可解决此问题,因为它可以确保它们永远不会是空字符串。
const timetableCells = timetable.Trains.map((item) => {
return {
cells: [item.Line || "N/A", item.Destination || "N/A", item.Car || "TBD", item.Min || "TDB"],
}
})
答案 1 :(得分:0)
我刚刚解决了非常类似的问题。您要分配给行和单元格的数组只能包含字符串。在那些是数字的情况下,将发生“无法解析DialogFlow响应int AppResponse的失败”。