coinbase / coinbase-pro-node

DEPRECATED — The official Node.js library for Coinbase Pro
Apache License 2.0
844 stars 316 forks source link

Ticker just returns null #359

Closed elbojoloco closed 5 years ago

elbojoloco commented 5 years ago

I follow the simple instructions from the documentation on this package and the api, but my basic script just returns null and exits:

const CoinbasePro = require('coinbase-pro')
const Client = new CoinbasePro.PublicClient()

Client.getProductTicker('BTC-USD', data => {
    console.log(data)
})

image

Even when keeping the script alive it does not show anything else than null once, what am I doing wrong?

vansergen commented 5 years ago

If you provide a callback function then the first argument is error. You can see the example here. The following code will show you the ticker (if there is no error)

Client.getProductTicker('BTC-USD', (error, response, data) => {
  console.log(data);
});
fb55 commented 5 years ago

Thanks @vansergen!

shimeilee commented 5 years ago

I tried the following but still got a null value.

publicClient.getProductTrades(instrument, (e, res, data) => {
                console.log("this is snapshot_data");
                console.log(e)
 })

Also, are we required to add a header (with the API secret) to get public data? I've followed the API GET request format on https://docs.pro.coinbase.com/#get-trades but I get a "CB-ACCESS-KEY header is required" error.

Thanks in advance! :)

vansergen commented 5 years ago

@shimeilee, there is no need to add headers (the library does it for you). You got null value because there was no error in your request (as I wrote in the previous answer). It simply means that you made a successful request. I suggest you read REAMDE. It contains some examples. So, if you want to get the trades you can do something like this (using callbacks)

publicClient.getProductTrades('ETH-BTC', (error, response, data) => {
  if (error) {
    // handle errors here
    console.error(error);
  } else {
    // work with data here
    console.log(data);
  }
});

or with async/await

let myFunc = async function() {
  try {
    console.log(await publicClient.getProductTrades('ETH-BTC'));
  } catch (error) {
    console.error(error);
  }
};
myFunc();
shimeilee commented 5 years ago

Thank you! My bad on my carelessness.

elbojoloco commented 5 years ago

Thanks for the info everyone :)