coinbase-node icon indicating copy to clipboard operation
coinbase-node copied to clipboard

Promisification trouble: TypeError: Cannot read property '_getOneHttp' of undefined

Open dsernst opened this issue 10 years ago • 8 comments

When I try to promisify this library with bluebird, I get the following error:

Unhandled rejection TypeError: Cannot read property '_getOneHttp' of undefined

This happens from bluebird's promisifyAll, and from manual promisification:

Unhandled rejection TypeError: Cannot read property '_getOneHttp' of undefined
    at Client.getAccount (/Users/dsernst/Documents/coinbase-fu/node_modules/coinbase/lib/Client.js:65:7)
    at tryCatcher (/Users/dsernst/Documents/coinbase-fu/node_modules/bluebird/js/release/util.js:11:23)
    at ret (eval at <anonymous> (/Users/dsernst/Documents/coinbase-fu/node_modules/bluebird/js/release/promisify.js:184:12), <anonymous>:13:39)
    at useCoinbase (/Users/dsernst/Documents/coinbase-fu/send-payments.js:123:45)

Here's the invoked line 123, the final line listed in the above stack trace, that leads to this problem:

return Promise.promisify(coinbase.getAccount)(coinbaseAccountId)
  .then(account => {

I've found I was able to fix this by changing it to:

return Promise.fromCallback(function(cb) {
  coinbase.getAccount(coinbaseAccountId, cb)
})
.then(account => {

The issue is resolved if I use this manual Promise.fromCallback method every time, but it would still be nice if this library could be improved to not require this, and we could just use .promisifyAll once when the library is first imported.

I also wanted to post this issue in case anyone else runs into the same problem.

Thanks 😄

dsernst avatar May 13 '16 06:05 dsernst

var Promise = require('bluebird');
var Client = require('coinbase').Client;
var bitcoin = new Client({
  'apiKey': xxxxxxxxx,
    'apiSecret': xxxxxxx,
    'baseApiUri': xxxxxxxx,
    'tokenUri': xxxxxxxxxxxx
});
return Promise.fromCallback(function(cb) {
    bitcoin.getSpotPrice({'currency': 'INR'}, cb)
  })
  .then(handleEntityNotFound(res))
  .then(responseWithResult(res))
  .catch(handleError(res));

got error message as follow:

TypeError: Promise.fromCallback is not a function

rajweb avatar May 17 '16 05:05 rajweb

Why not use something like this

function GetBTC(Callback){
    Wallet.getAccounts({}, function(Error, Accounts) {
        if(Error) return console.log(Error);
        return Callback(Accounts[0].balance.amount);
    });
}

GetBTC(BTC => console.log(`I have ${BTC} BTC`));

lewisgibson avatar Oct 29 '16 17:10 lewisgibson

The error comes from a context change. This will work:

// I prefer to use my own, but the solution will be the same with Bluebird
function promisify(errFirstCallback) {
  const fun = function() {
    const promise = new window.Promise(function(resolve, reject) {
      errFirstCallback(arguments, function(err, result) {
        if (err) {
          reject(err);
        }
        resolve(result);
      });
    });

    return promise;
  };

  return fun;
}

// This is the fix
promisify(client.getAccounts.bind(client));

Ideally, promises would be supported. See: https://github.com/coinbase/coinbase-node/issues/76

givanse avatar Mar 12 '17 07:03 givanse

@rajweb: Promise.fromCallback is from the bluebird library. Sorry for the confusion.

@Robinlemon: Your solution works, sure, with the drawback of not Promisify-ing the methods 😄. I'm not working anymore on the project that needed this, so tbh I don't fully remember the context. But, if I had to guess, the coinbase API was being used in a big chain of Promises, so using a callback to handle asynchrony in the middle of it would break consistency and make the code less understandable. Nonetheless, your code looks like it would work. And just fyi, bluebird.fromCallback is shorthand for what you're proposing, and returns a Promise.

@givanse: Thanks for the .bind tip. 😄

dsernst avatar Mar 12 '17 20:03 dsernst

Until there's a real support for promises, you can do this:

const { promisify } = require('util');

const getAccountsAsync = promisify(client.getAccounts).bind(client);

tleunen avatar Dec 08 '17 02:12 tleunen

You can also promisify the whole client by doing the following before calling new:

const { promisify } = require('util')
const { Client } = require('coinbase')

Object.keys(Client.prototype).forEach(method => {
  Client.prototype[method] = promisify(Client.prototype[method])
})

leonidez avatar Dec 30 '17 06:12 leonidez

You can also do this manually

const Promise = require('bluebird');

function GetBTC(){
    return new Promise((Resolve, Reject) => {
        return Wallet.getAccounts({}, function(Err, Accounts) {
            if(Err) return Reject(Err);
            return Resolve(Accounts[0].balance.amount);
        });
    });
}

GetBTC().then(BTC => console.log(`I own ${BTC} BTC`)).catch(console.error);

lewisgibson avatar Dec 30 '17 15:12 lewisgibson

I'm going to close this issue for now.

If you want it re-opened feel free to say something, but I feel weird being responsible for this open issue since I'm no longer working on the project that needed this.

Cheers

dsernst avatar Jan 05 '18 05:01 dsernst