3 Replies Latest reply: Feb 11, 2016 1:25 AM by Joel Lau RSS

    Qlik Sense Mashup - Referencing Hypercube matrix in another js function

    Joel Lau

      Hi all,

       

      I am currently trying to return the qMatrix to another js function within my html file (please see code below).  However, when I execute the code it returns with an undefined error.  Could anyone shed some light as to why this is the case?

       

      function returnMatrix(){

        require( ["js/qlik"], function ( qlik ) {

       

              qlik.setOnError( function ( error ) {

          alert( error.message );

          } );

             

          var app = qlik.openApp('338396bb-18b5-459b-8402-9b7f02ffaf80', config);

             

        app.createCube({

          "qDimensions": [{ 

                      qDef : { 

                          qFieldDefs : ["Air_Origin Code"] 

                      } 

                  },{ 

                      qDef : { 

                          qFieldDefs : ["Origin_Latitude"] 

                      } 

                  },{ 

                      qDef : { 

                          qFieldDefs : ["Origin_Longitude"] 

                      } 

                  },{ 

                      qDef : { 

                          qFieldDefs : ["Origin_Name"] 

                      } 

                  },{ 

                      qDef : { 

                          qFieldDefs : ["Air_Destination City Code"] 

                      } 

                  },{ 

                      qDef : { 

                          qFieldDefs : ["Dest_Latitude"] 

                      } 

                  },{ 

                      qDef : { 

                          qFieldDefs : ["Dest_Longitude"] 

                      } 

                  },{ 

                      qDef : { 

                          qFieldDefs : ["Dest_Name"] 

                      } 

                  }             

                  ],

       

                 "qInitialDataFetch": [{

                 "qHeight": 20,

                 "qWidth": 8

             }]

          }, function(reply){

       

       

             matrix = reply.qHyperCube.qDataPages[0].qMatrix;     

             originLat = matrix[0][1].qText

              originLong = parseFloat(matrix[0][2].qText)

              originName = parseFloat(matrix[0][3].qText)

            

            // console.log(matrix);

             return matrix;

        })

          

      });

       

      }

       

      HTML code

       

      <script>

         

                  var matrix = returnMatrix();

                 

                  console.log(matrix);

      </script>

        • Re: Qlik Sense Mashup - Referencing Hypercube matrix in another js function
          Erik Wetterberg

          Hi Joel,

          This is because the callback function is run asynchronously, after some (very fast, but still) communication with the engine. So your console.log will be run before you get the matrix. If you set breakpoints in the debugger you will see this clearly.

           

          You cannot return the matrix from your function, instead you need to call the function using the matrix inside the callback function.

           

          Hope this helps

           

          Erik

          • Re: Qlik Sense Mashup - Referencing Hypercube matrix in another js function
            Peter Cappelle

            Can i ask why you are putting javascript code inside your html? It would advice to keep your javascript code in at least one javascript files but leave it out of your html.

             

            I would do something like this...

             

            app.createCube({
                "qDimensions": [{
                            qDef : {
                                qFieldDefs : ["Air_Origin Code"]
                            }
                        },
                ...
                ...
                }, callback{
            
                })
               
                ...
                // Funther inside of your code
               ...
            
                function callback(reply, app){
            
                // this is the same as if you do function(reply){ 
                // If you do it this way it is more structured
            
            
                someFunction(matrix);  // in your callback you can call another self made function and pass your matrix as an argument.     // matrix now is not undefined anymore in someFunction because someFunction is called when there is reply data .
            
                }
            
            • Re: Qlik Sense Mashup - Referencing Hypercube matrix in another js function
              Joel Lau

              Thanks Erik / Peter. 

               

              Both replies were very helpful to my understanding on how to use hyper cubes. 


              Cheers,

              Joel