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.

    Synchronous requests [solved]

    Scheduled Pinned Locked Moved Development
    14 Posts 2 Posters 10.0k Views 1 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.
    • paviroP Offline
      paviro
      last edited by paviro

      I am just working on some changes in my Wunderlist module and have a problem caused by not knowing JavaScript enough…

      One instance of my module can display multiple Wunderlist lists. In order for that to work I have to do one requests for every list to the Wunderlist API.

       request({url: "https://a.wunderlist.com/api/v1/tasks?list_id=" + id, method: "GET", headers: {"X-Access-Token": this.config.accessToken,"X-Client-ID": this.config.clientID}}, function(error, response, body) {
            if (!error && response.statusCode == 200) {
            ....
            callback(tasks);
       }
      });
      

      My problem is I can’t think of a way to combine the todos from all those requests into a single array and then after all todos are received send them to the interface.

      We're all stories in the end. Just make it a good one, eh?

      – The Doctor

      1 Reply Last reply Reply Quote 0
      • MichMichM Offline
        MichMich
        last edited by

        Take a look at how I did this with the newsfeed.

        There is a Fetcher object that gets instantiated for every newsfeed. As soon as this feed receives new data it stores it in an instance variable and calls a function of the node_helper (see line 52 of the node helper).

        That function then retrieves the the values of all the Fetchers’ instance variables and combines the data. It then sends the socket notification with the combined data.

        1 Reply Last reply Reply Quote 0
        • paviroP Offline
          paviro
          last edited by

          I won’t pretend I completely understand what you did there :sweat_smile:
          Is it okay if I copy that code, I managed to modify it in a way that seems to work :)

          We're all stories in the end. Just make it a good one, eh?

          – The Doctor

          MichMichM 1 Reply Last reply Reply Quote 0
          • MichMichM Offline
            MichMich @paviro
            last edited by

            @paviro Yeah, but try to understand what I did. Thats the best way to learn. Let me know what you don’t understand. I’ll explain.

            1 Reply Last reply Reply Quote 0
            • paviroP Offline
              paviro
              last edited by

              I do not quite understand how the itemsReceivedCallbackworks :confused:

              We're all stories in the end. Just make it a good one, eh?

              – The Doctor

              1 Reply Last reply Reply Quote 0
              • paviroP Offline
                paviro
                last edited by

                Does work great now! The only problem is sometimes a list is empty on a refresh even tough it should not be :/

                { inbox: [],
                  'ViRO Entertainment': 
                   [ 'Todo1',
                     'Todo2',
                     'Todo3' ]
                 }
                

                We're all stories in the end. Just make it a good one, eh?

                – The Doctor

                1 Reply Last reply Reply Quote 0
                • MichMichM Offline
                  MichMich
                  last edited by

                  I’m on my phone now, so not really able to explain the callbacks. Will do that later.

                  Regarding the empty lists: try debugging by putting some log statements on places where You receive and send data.

                  1 Reply Last reply Reply Quote 0
                  • paviroP Offline
                    paviro
                    last edited by

                    It is not the socket connection. The fetcher sometimes returns an empty list :/

                    We're all stories in the end. Just make it a good one, eh?

                    – The Doctor

                    MichMichM 1 Reply Last reply Reply Quote 0
                    • MichMichM Offline
                      MichMich @paviro
                      last edited by

                      @paviro try debugging the Fetchet on the same way. ;)

                      1 Reply Last reply Reply Quote 0
                      • paviroP Offline
                        paviro
                        last edited by paviro

                        I am on it but I can’t find the problem… :confounded:
                        When broadcastItems is fired and therefore fetcher.onReceive triggered the lists are populated. But once broadcastTodos() collects the tasks by looping over the fetchers some lists appear to be empty. Any chance you could have a look?

                        We're all stories in the end. Just make it a good one, eh?

                        – The Doctor

                        1 Reply Last reply Reply Quote 0
                        • MichMichM Offline
                          MichMich
                          last edited by

                          Will fork your module tomorrow and give it a try.

                          1 Reply Last reply Reply Quote 0
                          • paviroP Offline
                            paviro
                            last edited by

                            Thanks! :)

                            We're all stories in the end. Just make it a good one, eh?

                            – The Doctor

                            1 Reply Last reply Reply Quote 0
                            • MichMichM Offline
                              MichMich
                              last edited by MichMich

                              Ok, I looked into it, and it really is pretty simple and obvious when I’ll explain you. So prepare for a “Doh!” moment … ;)

                              Right before you do the request to WunderList, you empty the items array:
                              https://github.com/paviro/MMM-Wunderlist/blob/master/fetcher.js#L38

                              Then if Wunderlist responds, you fill the items array with new items.

                              This means that as long as your fetcher is waiting for wunderlist to respond, the items array is empty. If in the meantime an other fetcher is finished fetching, it uses the items array to make a full list. But that array is then still empty, waiting to be filled after the wunderlust response.

                              The solution? Move line 38 (items = [];) to line 51 (above for (var i = 0; i < JSON.parse(body).length; i++) {).

                              Thats all! :)

                              1 Reply Last reply Reply Quote 1
                              • paviroP Offline
                                paviro
                                last edited by

                                And there comes the “Doh!” moment! :D Thanks completely overlooked that…

                                We're all stories in the end. Just make it a good one, eh?

                                – The Doctor

                                1 Reply Last reply Reply Quote 1

                                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                With your input, this post could be even better 💗

                                Register Login
                                • 1 / 1
                                • First post
                                  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