簡體   English   中英

無法將Dialogflow響應解析為AppResponse

[英]Failed to parse Dialogflow response into AppResponse

我在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我將得到一個如下所示的數組,並將其分配給conv.ask中的rows ,該rows應該等於Google Assistant根據其文檔所期望的行。

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

如果我將傳遞給conv.ask的JSON對象字符串化, conv.ask它看起來是正確的:

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"],
  }
})

我剛剛解決了非常類似的問題。 您要分配給行和單元格的數組只能包含字符串 在那些是數字的情況下,將發生“無法解析DialogFlow響應int AppResponse的失敗”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM