Object.assign не объединяет последнее значение

У меня есть этот вывод:

initial 0
+ 250
= 250
______________
initial 250
+ 176
= 250
______________

Но окончательный ответ должен быть 426. Кто-нибудь знает в чем проблема?

Это мой код:

app.get('/feed', (req, res) => {
    var opt = {
        method: 'GET',
        uri: 'url',
        headers: {
            'token': tkn
        },
        json: true
    };

    request(opt)
    .then((shopResponse) => {
        t = shopResponse.count;
        for (let i=1;i<=Math.round(t/250,0);i++) {
            gp(i,p,t);
        }
    });
}); 

const gp = async (i,p,t) => {
    var opt = {
        method: 'GET',
        uri: 'url',
        headers: {
            'token': tkn
        },
        json: true
    };
    const response = await request(opt);

    console.log("initial "+Object.keys(p).length);
    console.log("+ "+Object.keys(response.products).length);
    p = Object.assign(p, response.products);
    console.log("= "+Object.keys(p).length);
    console.log("______________");

    //once i have all data stored in p ... do something
    if ( Object.keys(p).length == t ) { 
        //code
    }
}

Вот как выглядит мой ответ:

 {
  "id": 632910392,
  "title": "IPod Nano - 8GB",
  "body_html": "<p>It's the small iPod with one very big idea: Video. Now the world's most popular music player, available in 4GB and 8GB models, lets you enjoy TV shows, movies, video podcasts, and more. The larger, brighter display means amazing picture quality. In six eye-catching colors, iPod nano is stunning all around. And with models starting at just $149, little speaks volumes.</p>",
  "vendor": "Apple",
  "product_type": "Cult Products",
  "created_at": "2018-10-31T13:02:19-04:00",
  "handle": "ipod-nano",
  "updated_at": "2018-10-31T13:02:19-04:00",
  "published_at": "2007-12-31T19:00:00-05:00",
  "template_suffix": null,
  "tags": "Emotive, Flash Memory, MP3, Music",
  "published_scope": "web",
  "admin_graphql_api_id": "gid://shopify/Product/632910392",
  "variants": [
    {
      "id": 808950810,
      "product_id": 632910392,
      "title": "Pink",
      "price": "199.00",
      "sku": "IPOD2008PINK",
      "position": 1,
      "inventory_policy": "continue",
      "compare_at_price": null,
      "fulfillment_service": "manual",
      "inventory_management": "shopify",
      "option1": "Pink",
      "option2": null,
      "option3": null,
      "created_at": "2018-10-31T13:02:19-04:00",
      "updated_at": "2018-10-31T13:02:19-04:00",
      "taxable": true,
      "barcode": "1234_pink",
      "grams": 567,
      "image_id": 562641783,
      "weight": 1.25,
      "weight_unit": "lb",
      "inventory_item_id": 808950810,
      "inventory_quantity": 10,
      "old_inventory_quantity": 10,
      "requires_shipping": true,
      "admin_graphql_api_id": "gid://shopify/ProductVariant/808950810",
      "presentment_prices": [
        {
          "price": {
            "currency_code": "USD",
            "amount": "199.00"
          },
          "compare_at_price": null
        }
      ]
    },
    {
      "id": 49148385,
      "product_id": 632910392,
      "title": "Red",
      "price": "199.00",
      "sku": "IPOD2008RED",
      "position": 2,
      "inventory_policy": "continue",
      "compare_at_price": null,
      "fulfillment_service": "manual",
      "inventory_management": "shopify",
      "option1": "Red",
      "option2": null,
      "option3": null,
      "created_at": "2018-10-31T13:02:19-04:00",
      "updated_at": "2018-10-31T13:02:19-04:00",
      "taxable": true,
      "barcode": "1234_red",
      "grams": 567,
      "image_id": null,
      "weight": 1.25,
      "weight_unit": "lb",
      "inventory_item_id": 49148385,
      "inventory_quantity": 20,
      "old_inventory_quantity": 20,
      "requires_shipping": true,
      "admin_graphql_api_id": "gid://shopify/ProductVariant/49148385",
      "presentment_prices": [
        {
          "price": {
            "currency_code": "USD",
            "amount": "199.00"
          },
          "compare_at_price": null
        }
      ]
    }
    /annnd way more elements

Прежде чем кто-то спросит: я не могу получить все данные за один запрос, поэтому я использую этот цикл.

Еще один вопрос: почему он завершает весь цикл, а затем начинает решать функцию обратного вызова? Я сделал что-то не так? Или как это должно произойти?

Спасибо!

0 ответов

Другие вопросы по тегам