Search code examples
javascriptnode.jsbotframework

Why data is doubled, how I can fix it?


Good afternoon.

I have required json, which contains the basic template for the layout of Adaptive Cards (bot framework).

Here's this 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\"}"
          }
        }
      }
    ]
  }

I also have a loop that takes data from my source and forms another json from it.

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++;
                
            }

Then I combine these json.

This works well, but when you retrieve the data again, the data is doubled.

How can this be resolved?

Thanks in advance.


Solution

  • It looks like every time you create a new card, you are appending the JSON object you created in the while loop to your required JSON object. Consequently, the next time you try to create a new card, the data from the previous request is still stored in your required JSON object. To avoid this, create a copy of the required JSON Object and store it in a different variable before your while loop. You can use JSON.parse(JSON.stringify(obj)) to create a copy of a JSON Object.

    Your code should look something like this:

    // 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)]
            });