Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用google actions sdk在google assistant应用程序中迭代js对象数组和显示列表选择器_Javascript_Android_Arrays_Node.js_Actions On Google - Fatal编程技术网

Javascript 如何使用google actions sdk在google assistant应用程序中迭代js对象数组和显示列表选择器

Javascript 如何使用google actions sdk在google assistant应用程序中迭代js对象数组和显示列表选择器,javascript,android,arrays,node.js,actions-on-google,Javascript,Android,Arrays,Node.js,Actions On Google,我已经在这里搜索了所有的文档 在“列表选择器”的文档中,所有示例都使用静态和固定数量的数据。 对于一个真实的场景,我调用RestAPI并在js对象中获得响应,js对象包含一个对象数组,我希望迭代该数组并在列表选择器中显示该信息 下面是google assistant的actions sdk文档中给出的列表选择器示例 function list (app) { app.askWithList(app.buildRichResponse() .addSimpleResponse('Alright'

我已经在这里搜索了所有的文档

在“列表选择器”的文档中,所有示例都使用静态和固定数量的数据。 对于一个真实的场景,我调用RestAPI并在js对象中获得响应,js对象包含一个对象数组,我希望迭代该数组并在列表选择器中显示该信息

下面是google assistant的actions sdk文档中给出的列表选择器示例

function list (app) {
app.askWithList(app.buildRichResponse()
.addSimpleResponse('Alright')
.addSuggestions(
  ['Basic Card', 'List', 'Carousel', 'Suggestions']),
// Build a list
app.buildList('Things to learn about')
// Add the first item to the list
.addItems(app.buildOptionItem('MATH_AND_PRIME',
  ['math', 'math and prime', 'prime numbers', 'prime'])
  .setTitle('Math & prime numbers')
  .setDescription('42 is an abundant number because the sum of its ' +
    'proper divisors 54 is greater…')
  .setImage('http://example.com/math_and_prime.jpg', 'Math & prime numbers'))
// Add the second item to the list
.addItems(app.buildOptionItem('EGYPT',
  ['religion', 'egpyt', 'ancient egyptian'])
  .setTitle('Ancient Egyptian religion')
  .setDescription('42 gods who ruled on the fate of the dead in the ' +
    'afterworld. Throughout the under…')
  .setImage('http://example.com/egypt', 'Egypt')
)
// Add third item to the list
.addItems(app.buildOptionItem('RECIPES',
  ['recipes', 'recipe', '42 recipes'])
  .setTitle('42 recipes with 42 ingredients')
  .setDescription('Here\'s a beautifully simple recipe that\'s full ' +
    'of flavor! All you need is some ginger and…')
  .setImage('http://example.com/recipe', 'Recipe')
)
);
}
现在,当我在.addItems()之前的js对象数组上添加迭代器时,会显示syntex的错误,因为我们无法在它上面添加任何行


当我们不能在上面写任何东西时,我们如何在这里迭代并执行.addItems()?

一旦在app.askWithList()外部创建列表,就很容易了,如下所示

function parseAndShowMessages(app, data) {
let list = app.buildList('test Messages')
for (var i = 0; i < data.response.messages.length; i++) {
  list.addItems(app.buildOptionItem(data.response.messages[i].subject,'')
    .setTitle(data.response.messages[i].from_name)
    .setDescription(data.response.messages[i].subject + '  \n' + data.response.messages[i].body)
    .setImage(IMG_URL_PICTURE,data.response.messages[i].subject)
  )
}

if (data.response.messages.length > 1) {
  app.askWithList(app.buildRichResponse()
    .addSimpleResponse('Sure, Below are your messages.')
    .addSuggestions(['Appointments']),list);
}
}
函数parseAndShowMessages(应用程序、数据){
let list=app.buildList('测试消息')
对于(var i=0;i1){
app.askWithList(app.buildRichResponse()
.addSimpleResponse('当然,下面是您的消息。'))
.add建议([“约会”),列表);
}
}
上面的数据是RestAPI的response js对象,data.response.messages有一个js对象数组


注意:根据actions sdk,列表选择器中必须至少有两个标题不同的项目。

在app.askWithList()外部创建列表后,就很容易了,如下所示

function parseAndShowMessages(app, data) {
let list = app.buildList('test Messages')
for (var i = 0; i < data.response.messages.length; i++) {
  list.addItems(app.buildOptionItem(data.response.messages[i].subject,'')
    .setTitle(data.response.messages[i].from_name)
    .setDescription(data.response.messages[i].subject + '  \n' + data.response.messages[i].body)
    .setImage(IMG_URL_PICTURE,data.response.messages[i].subject)
  )
}

if (data.response.messages.length > 1) {
  app.askWithList(app.buildRichResponse()
    .addSimpleResponse('Sure, Below are your messages.')
    .addSuggestions(['Appointments']),list);
}
}
函数parseAndShowMessages(应用程序、数据){
let list=app.buildList('测试消息')
对于(var i=0;i1){
app.askWithList(app.buildRichResponse()
.addSimpleResponse('当然,下面是您的消息。'))
.add建议([“约会”),列表);
}
}
上面的数据是RestAPI的response js对象,data.response.messages有一个js对象数组

注意:根据actions sdk,列表选择器中必须至少有两个标题不同的项目