pilwon / node-yahoo-finance

Yahoo Finance historical quotes and snapshot data downloader written in Node.js
495 stars 123 forks source link

Error while trying the "historical" example #81

Closed Rafael-Silva-Oliveira closed 2 months ago

Rafael-Silva-Oliveira commented 3 years ago

I'm just trying to run the historical example provided on the mainpage and I keep getting this output:

Code:

var yahooFinance = require('yahoo-finance');

const api = yahooFinance.historical({
  symbol: 'AAPL',
  from: '2012-01-01',
  to: '2012-12-31',
  period: 'd'
}, function (err, quotes) {
  //...
});

console.log(api);

Output:

<ref *1> Promise [Object] {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined,
  _cancellationParent: <ref *2> Promise [Object] {
    _bitField: 1,
    _fulfillmentHandler0: [Function (anonymous)],
    _rejectionHandler0: undefined,
    _promise0: [Circular *1],
    _receiver0: undefined,
    _cancellationParent: Promise [Object] {
      _bitField: 1,
      _fulfillmentHandler0: [Function (anonymous)],
      _rejectionHandler0: undefined,
      _promise0: [Circular *2],
      _receiver0: undefined,...

What am I missing to get the code running? I've checked the dependencies and they're all functioning

Thank you in advance!

gadicc commented 3 years ago

Hey @Rafael-Silva-Oliveira

You need to decide if you want to get the data via callback or from a promise. You're mixing both.

So you either want:

// Callback
yahooFinance.historical(options, function callback(err, quotes) {
  if (err) throw err;
  console.log(quotes);
});

or

// Promise using await syntax
const quotes = await yahooFinance.historical(options);
// Traditional promise syntax
yahooFinance.historical(options).then(quotes => { console.log(quotes); });

Hope this helps :)

Also if you're just starting out you may want to check out https://github.com/gadicc/node-yahoo-finance2 which is the next version of this library, currently in beta but pretty well tested.

Rafael-Silva-Oliveira commented 3 years ago

Thank you so much for your help, @gadicc! I'll check the new version right now, I appreciate your help :)