I am trying to get the order book through bitfex's web sockets, but I get an error in the console.

var ws = new WebSocket("wss://api-pub.bitfinex.com/ws/2"); ws.onopen = function() { // ws.send(JSON.stringify({ event: 'conf', flags: 131072 })) let msg = JSON.stringify({ event: 'subscribe', channel: 'book', pair: 'tBTCUSD', freq: 'F1', len: '25', prec: 'P0' }) ws.send(msg) ws.onmessage = function(msg) { const BOOK = {} BOOK.bids = {} BOOK.asks = {} BOOK.psnap = {} BOOK.mcnt = 0 msg = JSON.parse(msg.data) // var response = JSON.parse(msg.data); // console.log(msg.data); const csdata = [] const bidsKeys = BOOK.psnap['bids'] const asksKeys = BOOK.psnap['asks'] for (let i = 0; i < 25; i++) { if (bidsKeys[i]) { const price = bidsKeys[i] const pp = BOOK.bids[price] csdata.push(pp.price, pp.amount) } if (asksKeys[i]) { const price = asksKeys[i] const pp = BOOK.asks[price] csdata.push(pp.price, -pp.amount) } } const csStr = csdata.join(':') const csCalc = CRC.str(csStr) if (csCalc !== checksum) { console.error('CHECKSUM_FAILED') } } } 

Error annot read property '0' of undefined on the line if (bidsKeys[i]) {

  • where does this code come from? what should he do? - Stranger in the Q
  • Why cycle to 25? It probably should be before bidsKeys.length . - Stepan Kasyanenko

0