Qlik Community

Qlik Sense Integration, Extensions, & APIs

Discussion board where members can learn more about Integration, Extensions and API’s for Qlik Sense.

Not applicable

Qlik Sense Mashup - Referencing Hypercube matrix in another js function

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>

Tags (2)
1 Solution

Accepted Solutions
erik_wetterberg
Honored Contributor

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

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

Erik Wetterberg
https://extendingqlik.upper88.com/
3 Replies
erik_wetterberg
Honored Contributor

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

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

Erik Wetterberg
https://extendingqlik.upper88.com/
petercappelle
New Contributor III

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

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 .

    }

Not applicable

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

Thanks Erik / Peter. 

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


Cheers,

Joel

Community Browser