Object item not updating after being updated from inside a fetch call

Viewed 65

i have a object that i want to update the 'marketBuyPrice' and 'marketSellPrice' entries for using data from a fetch call as follows:

//The object
const ores = {
  Hemorphite :{ name: 'Hemorphite', marketRef: '1231', marketBuyPrice: '', marketSellPrice: ''},
  Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: ''},
};

//The fetch call
   function getMarketPrices(oreType, id) {
    var marketUrl = 'https://api.evemarketer.com/ec/marketstat?typeid='+id+'&usesystem=30000142';
    fetch(marketUrl)
        .then(response => response.text())
        .then(str => (new DOMParser()).parseFromString(str, "text/xml"))
        .then(data => {

            var buyPrice = data.getElementsByTagName("percentile")[0].childNodes[0].nodeValue;
            var sellPrice = data.getElementsByTagName("percentile")[1].childNodes[0].nodeValue;
                
            //This is where the buy / sell prices get updated
            ores[oreType].marketBuyPrice = buyPrice;
            ores[oreType].marketSellPrice = sellPrice;

        })
   }    

This works if i then view the entire array using:

console.log(ores);

Which returns:

{Hemorphite: {…}, Veldspar: {…}}
Hemorphite: {name: "Hemorphite", marketRef: "1231", marketBuyPrice: "1004.87", marketSellPrice: "1678.93"}
Veldspar: {name: "Veldspar", marketRef: "1230", marketBuyPrice: "12.00", marketSellPrice: "20.70"}

However if i try to view the specific updated entry:

console.log(ores[oreType].marketBuyPrice);

it returns it as empty as if it hasnt been updated yet when viewing the entire object as above it shows it as updated.

Could anyone offer any insight as to why this may be please.

Many thanks


//Update
const ores = {
  Hemorphite :{ name: 'Hemorphite', marketRef: '1231', marketBuyPrice: '', marketSellPrice: '', refines: {isogen: '2.4', nocxium: '9'} },
  Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: ''},
};

const minerals = {
  Hemorphite :{ name: 'Hemorphite', marketRef: '1231', marketBuyPrice: '', marketSellPrice: '',},
  Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: '',},
};

    function getMarketPrices(oreType, id) {
    var marketUrl = 'https://api.evemarketer.com/ec/marketstat?typeid='+id+'&usesystem=30000142';
    fetch(marketUrl)
        .then(response => response.text())
        .then(str => (new DOMParser()).parseFromString(str, "text/xml"))
        .then(data => {

            var buyPrice = data.getElementsByTagName("percentile")[0].childNodes[0].nodeValue;
            var sellPrice = data.getElementsByTagName("percentile")[1].childNodes[0].nodeValue;
                
            ores[oreType].marketBuyPrice = buyPrice;
            ores[oreType].marketSellPrice = sellPrice;

        })
   }    

        console.log(ores);
        console.log((ores['Veldspar'].marketBuyPrice));
1 Answers

Looks like you forgot await:

//The object
const ores = {
  Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: ''},
};

//The fetch call
function getMarketPrices(oreType, id) {
  var marketUrl = 'https://api.evemarketer.com/ec/marketstat?typeid='+id+'&usesystem=30000142';
  fetch(marketUrl)
      .then(response => response.text())
      .then(str => (new DOMParser()).parseFromString(str, "text/xml"))
      .then(data => {
          var buyPrice = data.getElementsByTagName("percentile")[0].childNodes[0].nodeValue;
          var sellPrice = data.getElementsByTagName("percentile")[1].childNodes[0].nodeValue;

          //This is where the buy / sell prices get updated
          ores[oreType].marketBuyPrice = buyPrice;
          ores[oreType].marketSellPrice = sellPrice;
      })
  }

getMarketPrices('Veldspar', '1230');
// data not updated now..
console.log(1, ores); 
console.log(2, ores['Veldspar'].marketBuyPrice); 
// But when you open this object in chrome console, you see prices.

If you are logging primitives, console show you actual value at moment, but in modern browsers objects can be filled with data, which coming after object was logged to console:

enter image description here

Fixed:

(async () => {

//The object
const ores = {
  Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: ''},
};

//The fetch call
async function getMarketPrices(oreType, id) {
  var marketUrl = 'https://api.evemarketer.com/ec/marketstat?typeid='+id+'&usesystem=30000142';
  await fetch(marketUrl)
      .then(response => response.text())
      .then(str => (new DOMParser()).parseFromString(str, "text/xml"))
      .then(data => {
          var buyPrice = data.getElementsByTagName("percentile")[0].childNodes[0].nodeValue;
          var sellPrice = data.getElementsByTagName("percentile")[1].childNodes[0].nodeValue;

          //This is where the buy / sell prices get updated
          ores[oreType].marketBuyPrice = buyPrice;
          ores[oreType].marketSellPrice = sellPrice;
      })
}


await getMarketPrices('Veldspar', '1230');
console.log(1, ores); 
console.log(2, ores['Veldspar'].marketBuyPrice); 
// data updated.

  
})();

Ok, loop:

(async () => {

//The object
const minerals = {
  Veldspar :{ name: 'Veldspar', marketRef: '1230', marketBuyPrice: '', marketSellPrice: ''},
  Tritanium :{ name: 'Tritanium', marketRef: '34', marketBuyPrice: '', marketSellPrice: ''},
  Pyerite :{ name: 'Pyerite', marketRef: '35', marketBuyPrice: '', marketSellPrice: ''},
  Mexallon :{ name: 'Mexallon', marketRef: '36', marketBuyPrice: '', marketSellPrice: ''},
  Isogen :{ name: 'Isogen', marketRef: '37', marketBuyPrice: '', marketSellPrice: ''},
  Nocxium :{ name: 'Nocxium', marketRef: '38', marketBuyPrice: '', marketSellPrice: ''},
  Zydrine :{ name: 'Zydrine', marketRef: '39', marketBuyPrice: '', marketSellPrice: ''},
  Megacyte :{ name: 'Megacyte', marketRef: '40', marketBuyPrice: '', marketSellPrice: ''},
};

//The fetch call
async function getMarketPrices(type, id) {
  var marketUrl = 'https://api.evemarketer.com/ec/marketstat?typeid='+id+'&usesystem=30000142';
  await fetch(marketUrl)
      .then(response => response.text())
      .then(str => (new DOMParser()).parseFromString(str, "text/xml"))
      .then(data => {
          var buyPrice = data.getElementsByTagName("percentile")[0].childNodes[0].nodeValue;
          var sellPrice = data.getElementsByTagName("percentile")[1].childNodes[0].nodeValue;

          //This is where the buy / sell prices get updated
          minerals[type].marketBuyPrice = buyPrice;
          minerals[type].marketSellPrice = sellPrice;
      })
}

await Promise.all(
  Object.entries(minerals)
    .map(([name_double, {name, marketRef}]) =>
      getMarketPrices(name, marketRef)
    )
);
console.log(minerals); 


})();

Related