Read the statement by Michael Teeuw here.
Async Functions, Sockets, Arrays, and Figuring this out
-
Hello,
I am fairly new to Javascript, and I am working on developing my first MMM. I am trying to pass the values from an array into an existing stock ticker module. My array is populated dynamically with values from a user’s portfolio, thus the values inside the array are returned from an Async function. Here is the code snippet from the async function:(async() => {
const getAcctResult = await tdaclient.accounts.getAccount(configGetAcct);const acctPositions = await lod.get(getAcctResult, 'securitiesAccount.positions'); const acctSymbols = await lod.map(acctPositions, 'instrument.symbol'); acctSymbols.forEach(function(value) { if (value.length <= 4) { tickerArray.push(value); } });
I would like to pass the first 5 values from “tickerArray,” and feed them into this existing function in the node_helper.js file:
sendRequest: function (urls) { var self = this; var results = {}; async.eachSeries(urls, function(url, done) { request({ url: url, method: "GET" }, function (error, response, body) { if (!error && response.statusCode == 200) { var result = JSON.parse(body); if(result["Error Message"]) { console.log("No such symbol!"); } else { var meta = result["Meta Data"]; if(!meta){ return; } var data = result["Time Series (Daily)"]; var compName = meta["2. Symbol"]; var count = 0; for (var key in data) { if (!data.hasOwnProperty(key)) {continue;} var obj = data[key]; if(!results[compName]){ results[compName] = []; } results[compName].push(obj); count++; if(count == 2) { break; } } } } done(); }); }, function(err) { if (err) { throw err; } self.sendSocketNotification("STOCK_RESULT", results); }); },
Effectively, I aim to replace the variable “urls” with tickerArray. The URLs variable is passed in from the config file. By nature this data in the config file is static, and I would like to make it dynamic. This is probably too big of an ask, but if anyone here has a clue on how I can figure this out let me know.
The original module for the stock ticker is here:
https://github.com/hakanmhmd/MMM-Stock
Again, all I really want to do is replace the default array with a dynamic version from the result of my above async function. If this module simply won’t play nice, I can probably eventually just write my own. Thats said, it just seems like it would be a lot easier if I could swap the config data with my array.
Thanks for any help or thoughts you can offer me, and if you need more information let me know.
-
@jcsindelar doesn’t look too hard. u are just missing the step that builds the url string from the stock symbol.
the existing function assumes an array of urls.
instead of storing the stock symbol, store the url with the stock symbol. the array doesn’t care.
then after the foreach loop call the existing function, passing tickerArrayalso the acct symbols to tickerarray is checking the length of the stock symbol not the ticker array
-
@jcsindelar if I understand correctly you want to call stockdata from a dynamic and changing range of stocks.
In some modules the array is provided by the config with every call to node_helper, in some modules just at the start.Another option would be to include a notification in the stock ticker module and use that to change the array.
I’m currently working on a successor to MMM-AVStock that also has a ticker.
If you’re interested we could implement your request into the code.
Meaning: your module sends a notification “CHANGE_STOCKS” and my module receives it and acts accordingly.notificationReceived: function(noti, payload) { if (noti === "CHANGE_STOCKS") { this.config.symbols = payload this.sendSocketNotification("GET_STOCKDATA", payload) } }
However, consider that the actual config will obviously not be safed in case of restart. But I guess your module will do the rest.