2017-07-24 148 views
2

循環文件我一直在嘗試Microsoftbot.dialog(「showShirts」動態創建卡,在微軟機器人框架

function (session) { 
    var msg = new builder.Message(session); 
    msg.attachmentLayout(builder.AttachmentLayout.carousel) 
    msg.attachments([ 
     new builder.HeroCard(session) 
      .title("Classic White T-Shirt") 
      .subtitle("100% Soft and Luxurious Cotton") 
      .text("Price is $25 and carried in sizes (S, M, L, and XL)") 
      .images([builder.CardImage.create(session, 'http://petersapparel.parseapp.com/img/whiteshirt.png')]) 
      .buttons([ 
       builder.CardAction.imBack(session, "buy classic white t-shirt", "Buy") 
      ]), 
     new builder.HeroCard(session) 
      .title("Classic Gray T-Shirt") 
      .subtitle("100% Soft and Luxurious Cotton") 
      .text("Price is $25 and carried in sizes (S, M, L, and XL)") 
      .images([builder.CardImage.create(session, 'http://petersapparel.parseapp.com/img/grayshirt.png')]) 
      .buttons([ 
       builder.CardAction.imBack(session, "buy classic gray t-shirt", "Buy") 
      ]) 
    ]); 
    session.send(msg).endDialog(); 
}).triggerAction({ matches: /^(show|list)/i }); bot framework in node js, 
i saw this sample code in the documentation 

我的問題是,而不是手動鍵入新builder.HeroCard().. 。如何創建一個循環從一個JSON數組填充這個?

我已經試過這

var obj = require("./dummy_json"); 
msg.attachments([ 
    obj.shirts.forEach(function(data){ 
     new builder.HeroCard(session) 
      .title(data.title) 
      .subtitle(data.subtitle) 
      .text(data.text) 
      .images([builder.CardImage.create(session, data.image_path)]) 
      .buttons([ 
       builder.CardAction.imBack(session, data.title, "Buy") 
      ]) 
    },this) 
]); 
+0

總結呢? –

+0

嘿@EzequielJadib我嘗試在forEach中包裝「new builder.HeroCard()...」,並返回一個空的附件數組。 '{ 「類型」: 「消息」, 「attachmentLayout」: 「轉盤」, 「附件」:[], 「區域設置」: 「的en-US」, 「LOCALTIMESTAMP」:「2017-07 -24T20:26:31.414Z 「 」從「:{ 」ID「: 」0db08g0j3blgl1jfmc「, 」名「: 」博特「 }, 」收件人「:{ 」ID「:」 默認用戶」 「名」: 「用戶」 }, 「inputHint」: 「acceptingInput」, 「ID」:無效, 「replyToId」: 「1kf4ad6jjihlk7k73」 }' –

+0

添加你的新代碼,請 –

回答

2

的問題是使y你正在做循環,但似乎你沒有添加任何東西到數組中。

嘗試是這樣的:一個用於/的forEach調用內部

var attachments = []; 
var obj = require("./dummy_json"); 

obj.shirts.forEach(function(data) { 
    var card = new builder.HeroCard(session) 
        .title(data.title) 
        .subtitle(data.subtitle) 
        .text(data.text) 
        .images([builder.CardImage.create(session, data.image_path)]) 
        .buttons([ 
         builder.CardAction.imBack(session, data.title, "Buy") 
        ]) 

    attachments.push(card); 
},this) 

msg.attachments(attachments); 
+1

非常感謝你。 –