I call the function below:
const getOpenOrders = () => {
// ws_api ??= new WebSocket(binance.WEBSOCKET_API);
const params = {
apiKey: binance.API_KEY,
timestamp: Date.now()
};
const searchParams = new URLSearchParams({ ...params });
searchParams.sort();
const signature = binance.signature(searchParams.toString());
searchParams.append("signature", signature);
ws_api.send(JSON.stringify({
id: "openOrders_status",
method: "openOrders.status",
params: Object.fromEntries(searchParams)
}));
};
in the open event.
ws_api.on("open", () => {
getOpenOrders();
}
Then in the message event I try to do this:
ws_api.on("message", data => {
data = JSON.parse(data);
switch (data.id) {
case 'openOrders_status':
openOrders = { ...data };
openOrders.hasPrice = price => !!openOrders.result.find(openOrder => parseFloat(openOrder.price) === price);
break;
}
});
Then I call getOpenOrders()
when executionReport is executed.
This code seems to work well for hours but sometimes I get this error:
TypeError: Cannot read properties of undefined (reading ‘find’) at openOrders.hasPrice
It seems that openOrders variable is undefined but this is impossible because I can read data.id value. What’s wrong?
I thing that message event should return always a defined value.