0

我想通过在 CollectionBrowse 中使用 for 循环来动态显示项目。我正在使用下面的代码,但是当我使用它时,谷歌上的操作会返回错误。

const {
  conversation,
  Simple,
  Card,
  Image,
  Button,
  List,
  Link,
  Table,
  CollectionBrowse,
  Suggestion,
  Schema
}  = require('@assistant/conversation');
const functions = require('firebase-functions');
const app = conversation({debug:true});

app.handle('callApi', (conv) => {
  conv.add(new CollectionBrowse({
   items:[
        {
          title: 'Item #1',
          description: 'Description of Item #1',
          footer: 'Footer of Item #1',
          image: {
            url: 'https://developers.google.com/assistant/assistant_96.png',
          },
          openUriAction: {
            url: 'https://www.example.com',
          },
        },
        {
          title: 'Item #2',
          description: 'Description of Item #2',
          footer: 'Footer of Item #2',
          image: {
            url: 'https://developers.google.com/assistant/assistant_96.png',
          },
          openUriAction: {
            url: 'https://www.example.com',
          },
        },
     {
          title: 'Item #3',
          description: 'Description of Item #3',
          footer: 'Footer of Item #3',
          image: {
            url: 'https://developers.google.com/assistant/assistant_96.png',
          },
          openUriAction: {
            url: 'https://www.example.com',
          },
        }
      ]
  }));
});

exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);

我想将我的代码更改为以下示例中的内容。

conv.add(new CollectionBrowse({
   items:[
        for(var i = 1; i < 4; i++){   <------------------------- i want it!!!!!!
         {
           title: 'Item #'+i,
           description: 'Description of Item #'+i,
           footer: 'Footer of Item #'+i,
           image: {
             url: 'https://www.example.com',
           },
           openUriAction: {
             url: 'https://www.example.com',
           },
         }
       }
      ]
  }));

对于for循环语句我该怎么办,如果不是这样,还有其他方法吗?

谢谢你。

4

1 回答 1

2

我自己找到了答案!

您可以使用循环创建数组,然后使用该数组创建新的 CollectionBrowse 对象。

const {
  conversation,
  Simple,
  Card,
  Image,
  Button,
  List,
  Link,
  Table,
  CollectionBrowse,
  Suggestion,
  Schema
}  = require('@assistant/conversation');
const functions = require('firebase-functions');
const app = conversation({debug:true});

app.handle('callApi', (conv) => {
  
  var titleArr = ['Item #1','Item #2','Item #3'];
  var descriptionArr = ['Description #1','Description #2','Description #3'];
  var footerArr = ['footer #1','footer #2','footer #3'];
  var imageArr = [{url: 'https://developers.google.com/assistant/assistant_96.png'},{url: 'https://developers.google.com/assistant/assistant_96.png'},{url: 'https://developers.google.com/assistant/assistant_96.png'}];
  var openUriActionArr = [{url: 'https://www.example.com'},{url: 'https://www.example.com'},{url: 'https://www.example.com'}];
  var itemsArr = [];
  
  for(var i = 0; i<3; i++){
    itemsArr.push({
      title : titleArr[i],
      description : descriptionArr[i],
      footer : footerArr[i],
      image : imageArr[i],
      openUriAction : openUriActionArr[i]
    });
  }
    
  conv.add(new CollectionBrowse({
   items: itemsArr
  }));
});

exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);
于 2020-10-30T09:53:28.383 回答