0
useEffect( () => {
        let allCryptoFuturesSymbols = {}
        getOrderRestrictions().then((result) => {
            let listOfSymbols = result.symbols
            for (let num in listOfSymbols) {
                let elt = listOfSymbols[num]
                const symbol = elt["symbol"]
                Object.defineProperties(allCryptoFuturesSymbols, {
                    [symbol]: {
                        value: symbol,
                        enumerable: true
                    }
                });
        })
        console.log(allCryptoFuturesSymbols)
        console.log(JSON.stringify(allCryptoFuturesSymbols))
    }, [])

The output of the first log is correct and expected, and the second is an empty object: {}. If you stringify a manually-defined object there is no problem, but the object created via this for-loop does not behave the same way.

user11629
  • 109
  • 5
  • Can you show the full code with the loop? – skara9 Dec 24 '21 at 19:18
  • 1
    Title and shown code do not align. One talks about a (not shown) loop. The other shows defineProperties. And none of the code shows JSON.stringify being called. Include a *minimal reproduction case*, remove the non-relevant code, and update the title/question. – user2864740 Dec 24 '21 at 19:20
  • JavaScript has no dictionary type. What you're referring to is an Object. – Cjmarkham Dec 24 '21 at 19:21
  • @user2864740 I addressed your comments. – user11629 Dec 24 '21 at 20:21
  • 1
    Move the declaration of `allCryptoFuturesSymbols` into the `.then` callback. – yaakov Dec 24 '21 at 20:29
  • @YaakovAinspan that didn't work. The solution is to make sure the orderrestrictions await is fulfilled before the population of the symbols object begins. – user11629 Dec 25 '21 at 00:27

0 Answers0