我如何使用NodeJS coinbase pro api从主体键获取价格

时间:2019-12-27 17:16:01

标签: node.js coinbase-api

如何使用NodeJS coinbase pro api从主体密钥获取价格?   我正在尝试使用api命令

从body键获取价格

getProductTicker

以下是我到目前为止的内容,但是我不知道如何解析  正文键的价格。

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

        publicClient.getProductTicker('ETH-USD', function(err, obj) {
        console.log('ETH-USD it worked');

        console.log('Start of Entries\n');
        console.log(Object.entries(obj));
        console.log('End of Entries \n');
        console.log('Start of keys\n');
        console.log(Object.keys(obj));
        console.log('End of keys \n');

});

下面是输出。我正在尝试从主体键获取价格。

  _ended: true,
  _callbackCalled: true
}
],
[ 'toJSON', [Function: responseToJSON] ],
[ 'caseless', Caseless { dict: [Object] } ],
[
'body',
'{"trade_id":53067029,"price":"125.24","size":"0.07555643","time":"2019-12- 
 27T17:04:37.429725Z","bid":"125.23","ask":"125.24","volume":"55843.38198278"}'
 ]  
 ]

条目结尾

按键开始

 [
 '_readableState',   'readable',
 '_events',          '_eventsCount',
 '_maxListeners',    'socket',
 'connection',       'httpVersionMajor',
 'httpVersionMinor', 'httpVersion',
 'complete',         'headers',
 'rawHeaders',       'trailers',
 'rawTrailers',      'aborted',
 'upgrade',          'url',
 'method',           'statusCode',
 'statusMessage',    'client',
 '_consuming',       '_dumped',
 'req',              'request',
 'toJSON',           'caseless',
 'body'
 ]

按键结束

1 个答案:

答案 0 :(得分:0)

您需要解析主体,因为主体将以字符串形式出现。我不确定toJson方法是否也可以使用,但值得尝试。试试这个。

const CoinbasePro = require("coinbase-pro");
const publicClient = new CoinbasePro.PublicClient();

publicClient.getProductTicker("ETH-USD", function(err, obj) {
  const parsedBody = JSON.parse(obj.body);
  const price = parsedBody.price;
});