A tiny error in a node.js code. Need a fix!
Hi folks,
I am trying to figure out how to make a small app that for now should track Binance exchange prices. I want to change from Max which tickers to track. But I have a problem with the node.js code.
Any idea how I should go about doing this in order to have the same output? I have 2 of Max.post and now I get an error on the second one. Do you know what I am doing wrong?node.script: [ "Price of BTCUSDT: ", "6591.57000000" ]
node.script: [ "Price of BTCUSDT: ", null ]
It's missing the nmp package which can be found here https://www.npmjs.com/package/node-binance-api. But the code is basic for someone with more knowledge than me :)
And the node.js code is this: const path = require('path');
const Max = require('max-api');
var nodeBinanceApi = require("node-binance-api");
var tickerName; // Holds the ticker pair name (eg BTCUSDT)
const binance = require('node-binance-api')().options({
APIKEY: '<key>',
APISECRET: '<secret>',
useServerTime: true, // If you get timestamp errors, synchronize to server time at startup
test: true // If you want to use sandbox mode where orders are simulated
});
// This will be printed directly to the Max console
Max.post(`loaded the ${path.basename(__filename)} script`);
// Use the 'addHandler' function to register a function for a particular message
Max.addHandler("tickerNameSelector", (tickerName) => {
binance.prices(tickerName, (error, ticker) => {
Max.post("Price of BTCUSDT: ", ticker.BTCUSDT);
Max.post("Price of BTCUSDT: ", ticker.tickerName);
});
});
Thank you so much!!
ygreq
It works!!! Thank you so much!! :D
And I appreciate the info regarding how your solution works.
Raja covered most here I guess but I'd advise to avoid eval unless very very necessary. So yeah, just for reference you could use the following "bracket notation property accessor":
ticker[tickerName]
in order to access a property of the ticker object using a variable. For more context you can also head over to MDN for a more reference style explanation with examples: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics
And yeah, for completeness the full code again:
const path = require("path");
const Max = require("max-api");
const nodeBinanceApi = require("node-binance-api");
let tickerName; // Holds the ticker pair name (eg BTCUSDT)
const binance = require("node-binance-api")().options({
APIKEY: "<key>",
APISECRET: "<secret>",
// If you get timestamp errors, synchronize to server time at startup
useServerTime: true,
// If you want to use sandbox mode where orders are simulated
test: true
});
// This will be printed directly to the Max console
Max.post(`loaded the ${path.basename(__filename)} script`);
// Use the 'addHandler' function to register a function for a particular message
Max.addHandler("tickerNameSelector", (tickerName) => {
binance.prices(tickerName, (error, ticker) => {
Max.post("Price of BTCUSDT: ", ticker.BTCUSDT);
Max.post("Price of BTCUSDT: ", ticker[tickerName]);
});
});
Thank you, Florian. May I ask why not use eval?
Thank you for info, MC WRAPPIN R∆J∆!