• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Stocks

Scheduled Pinned Locked Moved Utilities
36 Posts 15 Posters 30.9k Views 14 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    alexyak @ZTA0796
    last edited by Dec 14, 2016, 1:01 AM

    @ZTA0796 The original code does a hardcoded substring on the result:

    var result = JSON.parse(body.substring(3, body.length));

    You probably don’t need to do that.

    Z 2 Replies Last reply Dec 14, 2016, 1:36 AM Reply Quote 0
    • Z Offline
      ZTA0796 @alexyak
      last edited by Dec 14, 2016, 1:36 AM

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • Z Offline
        ZTA0796 @alexyak
        last edited by Dec 16, 2016, 1:53 AM

        @alexyak Thanks! I have one more question about syntax that I’m having trouble with.

        I can pull the Yahoo JSON into the node_helper file and print it to the terminal without a problem. But when I try to reference it in the “getDom” function, I get nothing. I haven’t changed the code at the start of that function at all. But when it runs “this.result.length”, the length returned is 0.

        I’m not super-familiar with the “this” syntax, but was hoping you or someone else might have an idea of why the JSON you pass would work, but not the JSON that I pass?

        Thanks!

        A 1 Reply Last reply Dec 16, 2016, 2:05 PM Reply Quote 0
        • C Offline
          chrisfoerg
          last edited by Dec 16, 2016, 8:54 AM

          great! would also be good for rss feeds :-)

          1 Reply Last reply Reply Quote 0
          • A Offline
            alexyak @ZTA0796
            last edited by Dec 16, 2016, 2:05 PM

            @ZTA0796 Could you show your code?

            Z 1 Reply Last reply Dec 17, 2016, 2:40 PM Reply Quote 0
            • Z Offline
              ZTA0796 @alexyak
              last edited by Dec 17, 2016, 2:40 PM

              @alexyak Sure…see below.

              In the node_helper.js:

              getStocks: function (url) {
              var self = this;

                request({ url: url, method: 'GET' }, function (error, response, body) {
                    if (!error && response.statusCode == 200) {
                      
                        var result = JSON.parse(body);
                        console.log(result.query.results.row);  //prints the JSON as expected to the terminal
                        self.sendSocketNotification('STOCKS_RESULT', result);
                    }
                });
              

              },

              //Subclass socketNotificationReceived received.
              socketNotificationReceived: function(notification, payload) {
              if (notification === ‘GET_STOCKS’) {
              this.getStocks(payload);
              }
              }

              });

              in stocks.js:

              getDom: function() {
              
                  var wrapper = document.createElement("table");
                  wrapper.className = 'normal regular small';
                  
                  var count = 0;
              
                  var _this = this;
              
              	console.log("in the DOM function");
              	console.log("result is " + this);  //prints [object Object]
              	console.log("result is " + _this); //prints [object Object]
              	console.log("result length is " + this.result.length); //prints 0
              
                  if (this.result.length > 0){
                      this.result.forEach(function(stock) {
              			//since length=0, it never gets in this loop
                                         //
              			//
              },
              
              scheduleUpdate: function(delay) {
              	console.log("scheduled update check");
              	var nextLoad = this.config.updateInterval;
              	if (typeof delay !== "undefined" && delay >= 0) {
              		nextLoad = delay;
              	}
              
              	var self = this;
              	setInterval(function() {
              		self.getStocks();
              	}, nextLoad);
              },
              
              roundValue: function(value) {
                 return Math.round(value*100)/100; 
              },
              
              getStocks: function () {
                  var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20csv%20where%20url%3D%27http%3A%2F%2Fdownload.finance.yahoo.com%2Fd%2Fquotes.csv%3Fs%3D' + this.config.stocks + '%26f%3Dsl1d1t1c1ohgv%26e%3D.csv%27%20and%20columns%3D%27symbol%2Cprice%2Cdate%2Ctime%2Cchange%2Ccol1%2Chigh%2Clow%2Ccol2%27&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
                  this.sendSocketNotification('GET_STOCKS', url);
              },
              
              
              socketNotificationReceived: function(notification, payload) {
                  if (notification === "STOCKS_RESULT") {
              		console.log("notification received");
              		console.log(payload);  //prints Object { query: Object }
                      this.result = payload;
                      this.updateDom(self.config.fadeSpeed);
                  }    
              },
              
              1 Reply Last reply Reply Quote 0
              • Z Offline
                ZTA0796
                last edited by Dec 17, 2016, 8:53 PM

                Upon looking at it further-- in the “socketNotificationReceived: function(notification, payload)” function in the module.js file, it seems that when I run console.log(this.result), it outputs my JSON:

                Object { symbol: “MSFT”, price: “62.30”, date: “12/16/2016”, time: “4:00pm”, change: “-0.28”, col1: “62.95”, high: “62.95”, low: “62.12”, col2: “42453083” }

                When I go to the “getDom” function and run “console.log(this.result)”, I get:

                [object Object]

                I’m just not sure why that change occurs, but that seems to be my issue. Any help is massively appreciated!

                A 1 Reply Last reply Dec 18, 2016, 3:48 PM Reply Quote 0
                • A Offline
                  alexyak @ZTA0796
                  last edited by Dec 18, 2016, 3:48 PM

                  @ZTA0796 The object that you get int the stocks.js is the deserialized javascript object. In your case it’s going to be a single row object. You could just access the properties list this:

                  this.result.symbol, this.result.price, etc…

                  -Alex

                  Z 1 Reply Last reply Dec 18, 2016, 7:31 PM Reply Quote 0
                  • Z Offline
                    ZTA0796 @alexyak
                    last edited by Dec 18, 2016, 7:31 PM

                    @alexyak So that’s what I expected as well. Unfortunately, it is somehow getting messed up between the “socketNotificationReceived” function and the “getDom” function.

                    socketNotificationReceived: function(notification, payload) {
                        if (notification === "STOCKS_RESULT") {
                            this.result = payload;
                            console.log(this.result);  //prints my JSON correctly console
                            this.updateDom(self.config.fadeSpeed);
                        }       
                    

                    So the above works correctly. Below, however, it doesn’t seem to maintain and I can’t figure out why.

                    getDom: function() {
                    
                        var wrapper = document.createElement("table");
                        wrapper.className = 'normal regular small';
                        
                        var count = 0;
                    
                        var _this = this;
                    
                    	console.log("result is " + this.result); //prints "result is [object Object]"
                    	console.log("result length is " + this.result.length); //prints "result length is 0"
                    	console.log(this.symbol);  //prints "undefined"
                        }
                    

                    I apologize if I’m being thick, but I can’t figure out why it’s not working…

                    1 Reply Last reply Reply Quote 0
                    • Z Offline
                      ZTA0796
                      last edited by Dec 18, 2016, 8:30 PM

                      Ok, so I think i figured it out. Still not sure why it worked, but for some reason, running a console.log("descriptive text " + this) seems to corrupt or change the type of “this”. If I put two consecutive console.log() calls–first with descriptive text, second with just “this”–then it works. Basically in the example above, if I just comment out the first two console.log() calls, then it no longer prints “undefined” and instead prints “MSFT”. Sorry for all the trouble!

                      1 Reply Last reply Reply Quote 0
                      • 1
                      • 2
                      • 3
                      • 4
                      • 1 / 4
                      1 / 4
                      • First post
                        9/36
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy