MagicMirror² v2.4.1 is available! For more information about this release, check out this topic.
Please read the release notes carefully since this update requires adjustments to your Raspberry Pi configuration!

[MMM-FlightsAbove] Problem receiving and seing JSON from node_helper



  • Dear Fellow MM Developers,

    I’ve been trying to complete my new module called MMM-FlightsAbove for days. But I have not been able to see or get the JSON displayed in any way.

    One day I hope it will look something like this:
    0_1519747240852_mmm-tabulator_server_ff.png

    I did get some useful pointers by @tbbear but I was not able to fix it even with that info. I have now uploaded the code in hope that someone can find some hints to what the problem is, apart from me.

    A few things that I have noticed so far.

    • The flightradar24-client API is returning improper JSON that need to be fixed [EDIT: A JS Array of objects ], I tried with a function and also with JSON.stringify(). Now what appears to be sent from node_helper.js seem ok. However, I cannot see it or verify it.
    • The result from the API is a Promise and I guess that is the core of the troubles. I simply don’t have the skills to deal with that yet. (I’ve been reading a lot about that lately, but the examples provided are not helping my case.)
    • The JSON shown in the console.log now seem to have 2 square brackets, instead of one, I don’t know how to remove it or if that has any effect.
    • I use the word seem above, as I no longer trust what come out of console.log, since it seem to have some built-in formatting, depending on what it is. (For example, this works: console.log(obj), while this doesn’t: console.log("anystring" + obj))
    • Tabulate is running and waiting (in vain?) for data.
    • The code is using my MMM-Tabulate demo module as a template, and is working fine there, when using a JSON file as input.

    Could someone please have a look at my code and see if there is something obvious that sticks out? (And if you see something, please say something here or in the issue tracker.)

    https://github.com/E3V3A/MMM-FlightsAbove

    Many humble thanks in advance,
    E:V:A



  • I just want to clarify that the JSON from the API is not broken, but just doesn’t conform to standard. The difference is like this:

    # cat dirty.json
    
      { id: '1',
        somenumber: 112233,
        somestring: 'ABCD',
        somenull: null 
      },
      { id: '2',
        somenumber: 445566,
        somestring: 'EFGH',
        somenull: null
      }
    
    
    # cat clean.json
    
      { "id": "1",
        "somenumber": 112233,
        "somestring": "ABCD",
        "somenull": null
      },
      { "id": "2",
        "somenumber": 445566,
        "somestring": "EFGH",
        "somenull": null
      }
    

  • Module Developer

    @E3V3A Actually the API doesn’t return json. It returns an array of Javascript objects. You can very easy iterate over the array and access any properties of the objects in the array with dot syntax.

    const radar = require("flightradar24-client/lib/radar");
    
    radar(53, 13, 52, 14)
      .then((flights) => {
        flights.forEach((flight) => {
          console.log("id: " + flight.id + ", origin: " + flight.origin);
        })
      })
      .catch((err) => {
        console.error(err);
        process.exit(1);
      });
    


  • @raywo

    It returns an array of Javascript objects.

    Thanks! That is certainly important to know! 🙂 But I don’t see how that help me here… 😞

    As for the rest of the test code above, it’s probably not very helpful in my case, but it could be great for another module though. In my code I already have (according to console.log the right output to be fed.) But somehow my feed mechanism is not working and I’m not able to check it.

    For example, another dummy question:
    Why doesn’t the following console.log's print anything?

        // This comes from YOUR module, usually "node_helper.js"
        socketNotificationReceived: function(notification, payload) {
            console.log("=====> " + this.name + " received a socket notification: " + notification); //+ " - Payload: " + payload);
            switch (notification) {
                case "NEW_DATA":
                    console.log("-----> FlightsAbove: NEW_DATA received!"); // Why doesn't this show?
                    let ping = payload;
                    console.log("-- PING!\n");
                    console.log(ping);
                    //console.log("-- PING DATA:\n", ping);
    
                    this.loaded = true;
                    this.setTableData(payload);
                    break;
                default:
                    console.log("Did not match the Notification: ", notification);
            }
        },
    

    Similarly, here in node_helper.js. Everything before the //WTF comments prints, but the rest is never seen! ???

    
        radarPing: function() {
            console.log("ENTER (inside)");
            Promise.all([
                //radar(-8.20917,114.62177,-9.28715,115.71243)  // "PDS" (Bali Airport)
                radar(53.05959,12.52388,51.98161,14.29552) // (Berlin)
                ]).then(function(results) {
                    var ping = JSON.stringify(results);
    
                    console.log("Sending NEW_DATA:");
                    console.log(ping);
    
                    // WTF! This below is never shown!
                    this.sendSocketNotification("NEW_DATA", ping); //self?
                    console.log("Sent NEW_DATA!");
                    console.log("NEW_DATA is: %O", ping);
                    console.dir(ping, {depth: null, colors: true});
    
                    //return ping;
                }).then(function(error) {
                    //console.log("ERROR:")
                    console.log(error);
                });
            console.log("EXIT (inside)");
    },
    

    So at the end of the day, I have no idea if the NEW_DATA socket notification was actually sent, nor what was the payload, nor if it was received.


  • Moderator

    Inside the module itself console.log doesn’t work. Use Log.error instead and check the output in your browser console, not the terminal/cmd


  • Module Developer

    @yawns That is not correct. console.log() works fine in your module. But the output is not shown on the log of your mirror but on the browser’s log. You need to open the developer tools in your browser to see it.


  • Moderator

    Oh, really? Maybe this was changed, in the beginning it wasn’t working at all in the main module file.


  • Module Developer

    @E3V3A This function works just fine:

    radarPing: function () {
          radar(53.05959, 12.52388, 51.98161, 14.29552)
            .then((flights) => {
              this.sendSocketNotification("NEW_DATA", flights); //self?
              console.log("New radar data: ");
              console.log(flights);
            })
            .catch(function (error) {
              //console.log("ERROR:")
              console.log(error);
            });
        },
    

    It receives data, prints them on the mirror’s log and sends it to the module. The module is receiving the data and logs them to the browser’s console. And your table is populated too.



  • @yawns That’s yet another one! (And it didn’t give any output.) So far I have seen:

    Log.log()
    Log.info()
    console.log()
    console.error()
    

    Then I have seen more general node ones like:
    console.dir()

    How can we know where they can be used (and where the output goes)?


  • Module Developer

    @E3V3A console.log() is a standard Javascript function it works anywhere. Log.info() and so on are functions from the MagicMirror’s module system. So they will only work in the module file.

    node_helper.js is executed in the server context. Any console output is therefore on the server’s log.

    The module file is executed in the client context (the browser). So console output from the module file is logged in the browser console.