2016-12-28 94 views
0

我是一位初学javascript和编码的初学者。 我想要做的是从这个websocket数据源获得出价(数组),但我很挣扎,在我的代码中我试图打印(console.log)只有“bidPrice”,而不是其他所有东西“asksize”,“bidize”等等等 当我运行我的代码我只得到undefined,而不是“bidPrice”, 这里缺少什么? 感谢您的任何帮助从node.js中的websocket数据馈送中读取JSON数组

这里结果(CMD)当我运行代码不好!!!!

C:\Users\Desktop\codesource>node wss.js 
Connection opened 
undefined 
undefined 
undefined 

这里是我的Node.js代码:

这里从bitmex WebSocket的未来数据

Connection opened 
{ info: 'Welcome to the BitMEX Realtime API.',enter code here 
    version: '1.2.0', 
    timestamp: '2016-12-28T22:27:15.000Z', 
    docs: 'https://www.bitmex.com/app/wsAPI', 
    heartbeatEnabled: false } 
{ success: true, 
    subscribe: 'quote:XBTUSD', 
    request: { op: 'subscribe', args: [ 'quote:XBTUSD' ] } } 
{ table: 'quote', 
    keys: [], 
    types: 
    { timestamp: 'timestamp', 
    symbol: 'symbol', 
    bidSize: 'long', 
    bidPrice: 'float', 
    askPrice: 'float', 
    askSize: 'long' }, 
    foreignKeys: { symbol: 'instrument' }, 
    attributes: { timestamp: 'sorted', symbol: 'grouped' }, 
    action: 'partial', 
    data: 
    [ { timestamp: '2016-12-28T22:26:54.645Z', 
     symbol: 'XBTUSD', 
     bidSize: 12, 
     bidPrice: 969.59, 
     askPrice: 971.06, 
     askSize: 499 } ] } 

看来它不工作太

console.log(response.data[0].bidPrice); 


Connection opened 
C:\Users\jalal\Desktop\codesource\wss.js:24 
     console.log(response.data[0].bidPrice); 
           ^

TypeError: Cannot read property '0' of undefined 
    at WebSocket.<anonymous> (C:\Users\jalal\Desktop\codesource\wss.js:24:27) 
    at emitTwo (events.js:106:13) 
    at WebSocket.emit (events.js:191:7) 
    at Receiver.ontext (C:\Users\jalal\Desktop\codesource\node_modules\ws\lib\WebSocket.js:841:10) 
+0

感谢,但不工作,也许我错过出头其他 – itriad

回答

1

变化

console.log(json["bidPrice"]); 

console.log(response.data[0].bidPrice); 

FYI你不需要变量datajson

+1

确定他不应该做'response.data [o] .bidPrice'? – jcolebrand

+0

哎呀,是的,你是对的。我假设这里有太多东西.. –

+0

console.log(response.data [0] .bidPrice);不工作呢? – itriad