1

I am running into an issue with this code:

let productBuyDataArray = [];
    // productBuyData is a promise
    productBuyData.then(function(result) {
      for (let key in result) {
        productBuyDataArray.push({
          time: key,
          value: result[key],
        });
      }
    }
    );

when I console.log(productBuyDataArray);, it shows the data like I want it. But then when I try to use the data it is gone, the array is empty. I found out with console.log(JSON.stringify(productBuyDataArray)); that it is indeed empty. Can someone help me with this?

Olle
  • 81
  • 5
  • If you try `let productBuyDataArray = [];` `productBuyDataArray.then(...);` `some_other_code` then `some_other_code` is going to run before your `then(...)` so the array will be empty at that time. – qrsngky Jul 26 '22 at 09:19
  • can you show some more code, especially places with console.logs – Aziz Hakberdiev Jul 26 '22 at 09:24
  • @AzizHakberdiev No need. The problem is common and clear, the solution is provided in the questions linked at the top of this question. – Ivar Jul 26 '22 at 09:26

0 Answers0