alisabrigok / tcmb-exchange-rates

Get exchange rates announced by the Central Bank of the Republic of Turkey (as known as CBRT or TCMB) easily. - Türkiye Cumhuriyeti Merkez Bankası (TCMB) tarafından yayınlanan döviz kurlarını kolayca çekmenizi sağlayan kütüphane.
MIT License
12 stars 3 forks source link

Error on Data Fetching #1

Open Ardakilic opened 3 years ago

Ardakilic commented 3 years ago

Hello,

I get error on data fetching.

Totally fresh installation on node v 16 (v16.6.2 to be exact), both error appears on Linux and macOS.

npm cache is cleared before installation.

Here's the files:

{
  "dependencies": {
    "tcmb-exchange-rates": "^0.1.10"
  }
}
var tcmb = require('tcmb-exchange-rates');
tcmb()
    .then(function(data) {
        console.log(data);
    })
    .catch(function(error) {
        console.log(error);
    });

And here's the error when I run the package:

node:events:371
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read property 'Currency' of undefined
    at /Users/arda/projects/tcmb/node_modules/tcmb-exchange-rates/lib/index.js:280:79
    at Parser.<anonymous> (/Users/arda/projects/tcmb/node_modules/xml2js/lib/parser.js:304:18)
    at Parser.emit (node:events:394:28)
    at SAXParser.onclosetag (/Users/arda/projects/tcmb/node_modules/xml2js/lib/parser.js:262:26)
    at emit (/Users/arda/projects/tcmb/node_modules/sax/lib/sax.js:624:35)
    at emitNode (/Users/arda/projects/tcmb/node_modules/sax/lib/sax.js:629:5)
    at closeTag (/Users/arda/projects/tcmb/node_modules/sax/lib/sax.js:889:7)
    at SAXParser.write (/Users/arda/projects/tcmb/node_modules/sax/lib/sax.js:1436:13)
    at Parser.exports.Parser.Parser.parseString (/Users/arda/projects/tcmb/node_modules/xml2js/lib/parser.js:323:31)
    at Parser.parseString (/Users/arda/projects/tcmb/node_modules/xml2js/lib/parser.js:5:59)
Emitted 'error' event on Parser instance at:
    at Parser.exports.Parser.Parser.parseString (/Users/arda/projects/tcmb/node_modules/xml2js/lib/parser.js:327:16)
    at Parser.parseString (/Users/arda/projects/tcmb/node_modules/xml2js/lib/parser.js:5:59)
    at exports.parseString (/Users/arda/projects/tcmb/node_modules/xml2js/lib/parser.js:369:19)
    at Request._callback (/Users/arda/projects/tcmb/node_modules/tcmb-exchange-rates/lib/index.js:271:13)
    at Request.self.callback (/Users/arda/projects/tcmb/node_modules/request/request.js:185:22)
    at Request.emit (node:events:394:28)
    at Request.<anonymous> (/Users/arda/projects/tcmb/node_modules/request/request.js:1154:10)
    at Request.emit (node:events:394:28)
    at IncomingMessage.<anonymous> (/Users/arda/projects/tcmb/node_modules/request/request.js:1076:12)
    at Object.onceWrapper (node:events:513:28)

When I digged the code I realized the rejects don't have return, which continues to execute the code, which results the error.

Also, when I try to fetch the XML code with the library, this is what today.xml is returned as:

<html><body onload='resend();'>
<script>
function resend() {
    document.cookie="sto-idd=4062e083a73e53d0; max-age=1; path=/";
    window.location.reload();
}
</script>
</body>
</html>

So, I believe the promise rejects and the user agents for fetching can be optimized.

Thanks in advance,

Ardakilic commented 3 years ago

Since the last 2 weeks, there has been no issues. I believe it was TCMB's server's fluke. However, since the error was not handled I'm leaving this open. Feel free to close it.

alisabrigok commented 3 years ago

Sorry for the late response and thanks for reporting the issue.

It seems quite the case that rejections are not returned and the nested property access for objects is not very well handled.

I already want to clean up this package by rewriting with ESNext and ship polyfilled and also return the latest exchange rates prior to the public holidays instead of returning an error (with optionally enabled to not to have breaking changes) but unfortunately, life keeps getting in the way.

I'm happy that the package was useful to you and I'm glad that it works again after, seemingly, the missing data issue by TCMB was fixed.

For now, I'm leaving the issue open to come back to solve it later.