簡體   English   中英

為什么數據增加一倍,我該如何解決?

[英]Why data is doubled, how I can fix it?

下午好。

我需要json,其中包含用於Adaptive Card(機器人框架)布局的基本模板。

這是這個json:

 { "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "version": "1.0", "type": "AdaptiveCard", "speak": "", "body": [ { "type": "TextBlock", "horizontalAlignment": "center", "text": "Все машины", "weight": "bolder", "isSubtle": false }, { "type": "TextBlock", "text": "Внимание, вы вошли в режим тендера.", "separator": true } ], "actions": [ { "type": "Action.Submit", "title": "Подтвердить лот(ы)", "data": { "msteams": { "type": "messageBack", "displayText": "Идет подтверждение ваших лотов, ожидайте!", "text": "/accept", "value": "{\\"x\\": \\"bfVal\\", \\"y\\": \\"from value\\"}" } } }, { "type": "Action.Submit", "title": "Отменить все лоты", "data": { "x": "123", "msteams": { "type": "messageBack", "displayText": "Идет отменение ваших лотов, ожидайте!", "text": "/cancel", "value": "{\\"bfKey\\": \\"bfVal\\", \\"conflictKey\\": \\"from value\\"}" } } } ] } 

我也有一個循環,它從我的源中獲取數據並從中形成另一個json。

 try { // Pull in the data from Microsoft Graph. const client = new SimpleGraphClient(tokenResponse.token); const me = await client.getList(); var i = 0; while (i < me['value'].length) { feed = { "type": "ColumnSet", "separator": true, "columns": [ { "type": "Column", "width": 1, "items": [ { "type": "TextBlock", "text": "Продукт", "isSubtle": true }, { "type": "TextBlock", "size": "extraLarge", "color": "accent", "text": me.value[i].fields.Good, "spacing": "none" }, { "type": "TextBlock", "text": "Дата: " + dateFormat(me.value[i].fields.ShipmentDateTime, 'dd-mm-yyyy'), "spacing": "yes" } ] }, { "type": "Column", "width": "auto", "items": [ { "type": "TextBlock", "text": " " }, { "type": "Image", "url": "https://png.pngtree.com/svg/20170614/engine_oil_410031.png", "size": "medium", "spacing": "yes" }, { "type": "TextBlock", "text": " ID: " + me.value[i].fields.id, "value": me.value[i].fields.id } ] }, { "type": "Column", "width": 1, "items": [ { "type": "TextBlock", "horizontalAlignment": "right", "text": "RUB", "isSubtle": true }, { "type": "TextBlock", "horizontalAlignment": "right", "size": "extraLarge", "color": "accent", "text": me.value[i].fields.PricePerTon, "spacing": "none" }, { "type": "Input.Toggle", "title": "Приобрести лот", "valueOn": "true", "valueOff": "false", "id": "buyGood", "spacing": "yes" } ] } ] } tender.body.push(feed); i++; } 

然后我結合這些json。

這很好用,但是當您再次檢索數據時,數據將增加一倍。

如何解決?

提前致謝。

看起來每次創建新卡時,您都將在while循環中創建的JSON對象附加到所需的JSON對象。 因此,下次您嘗試創建新卡時,來自上一個請求的數據仍存儲在所需的JSON對象中。 為了避免這種情況,請創建所需JSON對象的副本,並將其存儲在while循環之前的其他變量中。 您可以使用JSON.parse(JSON.stringify(obj))創建JSON對象的副本。

您的代碼應如下所示:

// create a copy of the required JSON Object
// and store it in a new variable
const card = JSON.parse(JSON.stringify(tender));

var i = 0;
while (i < me['value'].length) {
    let feed = {
        "type": "ColumnSet",
        "separator": true,
        ...
        }

    // push feed to `card` instead of `tender`
    card.body.push(feed);
    i++;
}

... 

// send `card` to user instead of `tender`
await turnContext.sendActivity({
            text: "Double Data Card",
            attachments: [CardFactory.adaptiveCard(card)]
        });

暫無
暫無

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

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