2016-12-28 89 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