0

I am trying to access exchange rates on "chrome.storage.local" via a background script. I need to transfer this data from there to the content script. The problem is that if I return a constant value, I can get the data I want. But if I want to access local storage through the promise object, an undefined value is returned. Where exactly am I going wrong? How do I send a content script to the Promise object?

rates is a json object. It contains the current exchange rates of all currencies.

{"rates":{"USD":1,"AED":3.67,"AFN":110.29,"ALL":107.29,"AMD":481.09,"ANG":1.79,"AOA":570.19,"ARS":101.94,"AUD":1.4,"AWG":1.79,"AZN":1.7,"BAM":1.73,"BBD":2,"BDT":86.2,"BGN":1.73,"BHD":0.376,"BIF":1990.56,"BMD":1,"BND":1.37,"BOB":6.89,"BRL":5.7,"BSD":1,"BTN":75.85,"BWP":11.73,"BYN":2.53,"BZD":2,"CAD":1.29,"CDF":1998.08,"CHF":0.922,"CLP":844.2,"CNY":6.38,"COP":3997.65,"CRC":638.76,"CUC":1,"CUP":25,"CVE":97.74,"CZK":22.42,"DJF":177.72,"DKK":6.61,"DOP":56.92,"DZD":139.34,"EGP":15.71,"ERN":15,"ETB":49.01,"EUR":0.886,"FJD":2.13,"FKP":0.757,"FOK":6.61,"GBP":0.757,"GEL":3.09,"GGP":0.757,"GHS":6.59,"GIP":0.757,"GMD":52.9,"GNF":9502,"GTQ":7.74,"GYD":209.47,"HKD":7.8,"HNL":24.36,"HRK":6.68,"HTG":100.82,"HUF":326.62,"IDR":14371.41,"ILS":3.17,"IMP":0.757,"INR":75.85,"IQD":1461.33,"IRR":42149.4,"ISK":130.05,"JMD":154.14,"JOD":0.709,"JPY":113.57,"KES":113.14,"KGS":84.77,"KHR":4076.33,"KID":1.4,"KMF":436.07,"KRW":1189.51,"KWD":0.3,"KYD":0.833,"KZT":437.21,"LAK":11161.75,"LBP":1507.5,"LKR":202.49,"LRD":141.76,"LSL":15.79,"LYD":4.61,"MAD":9.17,"MDL":17.75,"MGA":3059.48,"MKD":54.48,"MMK":1786.2,"MNT":2870.53,"MOP":8.04,"MRU":36.36,"MUR":43.78,"MVR":15.37,"MWK":849.76,"MXN":20.79,"MYR":4.22,"MZN":64.06,"NAD":15.79,"NGN":412.28,"NIO":35.59,"NOK":9.05,"NPR":121.37,"NZD":1.49,"OMR":0.384,"PAB":1,"PEN":4.04,"PGK":3.5,"PHP":49.89,"PKR":177.93,"PLN":4.11,"PYG":6925.87,"QAR":3.64,"RON":4.39,"RSD":104.3,"RUB":74.21,"RWF":1041.38,"SAR":3.75,"SBD":7.93,"SCR":13.26,"SDG":437.89,"SEK":9.13,"SGD":1.37,"SHP":0.757,"SLL":11246.21,"SOS":579.21,"SRD":21.6,"SSP":425.25,"STN":21.72,"SYP":2520.6,"SZL":15.79,"THB":33.58,"TJS":11.28,"TMT":3.5,"TND":2.81,"TOP":2.28,"TRY":15.65,"TTD":6.8,"TVD":1.4,"TWD":27.84,"TZS":2307.82,"UAH":27.26,"UGX":3550.04,"UYU":44.48,"UZS":10920.77,"VES":4.6,"VND":22889.59,"VUV":113.19,"WST":2.62,"XAF":581.43,"XCD":2.7,"XDR":0.717,"XOF":581.43,"XPF":105.77,"YER":249.9,"ZAR":15.79,"ZMW":16.49}}

Content Script:

    marketCurrency = 'USD'

    chrome.runtime.sendMessage({content: marketCurrency, type: "get-rates"}, function (response){
         //USD Currency
         console.log(response)
    });

Background Script:

async function getCurrentRate () {
    return new Promise(async function (resolve) {
        chrome.storage.local.get('rates', function (result) {
            resolve(result['rates']);
        });
    });
}


chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
    if (message.type == 'get-rates') {
        mCurrency = message.content
        objAllRates = await getCurrentRate()
        UsdRate = objAllRates.rates[mCurrency]

        sendResponse(UsdRate);
    }
    
});

Thank you

hcttepe
  • 157
  • 1
  • 6
  • 11
  • 1
    What have you done so far to solve this on your own? What did you learn from debugging? Is there a value at `"rates"` in the local storage? Is that value an object with a `rates` property whose value is an object? Is there a property that matches the content of `mCurrency`? – Andreas Dec 21 '21 at 13:46
  • rates is a json object. It contains the current exchange rates of all currencies. The problem is that I cannot return the promise object. – hcttepe Dec 21 '21 at 13:58
  • 1
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Dec 21 '21 at 14:05

0 Answers0