Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

vue.js - Tableau Vuejs getWorkBook() "Cannot read property get_workbook of null"

I am trying to follow the getData example found on the tableau javascript tutorial (https://github.com/tableau/js-api-samples/blob/master/getDataBasic.html) , but for vue js, however, I am unable to get it to work. I am able to render the tableau object, but when it comes to getting the underlying data or even trying to get the workbook name, I get the error: "Cannot read property get_workbook of null". Below is my code:

<template>
    <div class="container" style="margin-top: 90px;">

        <div id="vizContainer2"></div>

    </div>
</template>

<script>
export default {
  name: 'TableauHolder',
  methods: {
        getUnderlyingData(){
            const containerDiv = document.getElementById("vizContainer2")
            let url = "http://public.tableau.com/views/RegionalSampleWorkbook/Storms"
            let options = {
                hideTabs: true,
                hideToolbar: true,
                onFirstInteractive: () => { 
                }
                
            }
            this.viz = new window.tableau.Viz(containerDiv, url, options)
            let sheet = this.viz.getWorkbook().getActiveSheet().getWorksheets().get("Storm Map Sheet")
            console.log(sheet)
        },
        
        
    },
    mounted () {
        window.addEventListener('load', () => {
            this.getUnderlyingData();
        })
    }
}

</script>

Placing getWorBbook() in onFirstInteractive successfully gets me the workbook name (as shown below), but I am not sure where to go from there in terms rendering the data.

<template>
    <div class="container" style="margin-top: 90px;">

        <div id="vizContainer2"></div>

    </div>
</template>

<script>
export default {
  name: 'TableauHolder',
  methods: {
        getUnderlyingData(){
            const containerDiv = document.getElementById("vizContainer2")
            let url = "http://public.tableau.com/views/RegionalSampleWorkbook/Storms"
            let options = {
                hideTabs: true,
                hideToolbar: true,
                onFirstInteractive: () => {
                    let sheet = this.viz.getWorkbook()
                    console.log(sheet)
                }
                
            }
            this.viz = new window.tableau.Viz(containerDiv, url, options)

        },
        
        
    },
    mounted () {
        window.addEventListener('load', () => {
            this.getUnderlyingData();
        })
    }
}

</script>
question from:https://stackoverflow.com/questions/65894977/tableau-vuejs-getworkbook-cannot-read-property-get-workbook-of-null

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I realized that the JavaScript API is asynchronous and therefore the let sheet line is executed before while executing the API. Therefore, something like setTimeout will make the line execute after the API has been executed. See below incase anyone was having similar issues:

<template>
    <div class="container" style="margin-top: 90px;">

        <div id="vizContainer2"></div>

    </div>
</template>

<script>
export default {
  name: 'TableauHolder',
  methods: {
        getUnderlyingData(){
            const containerDiv = document.getElementById("vizContainer2")
            let url = "http://public.tableau.com/views/RegionalSampleWorkbook/Storms"
            let options = {
                hideTabs: true,
                hideToolbar: true,
                onFirstInteractive: () => { 
                }
                
            }
            this.viz = new window.tableau.Viz(containerDiv, url, options)
            setTimeout(() => { 
                let sheet = this.viz.getWorkbook().getActiveSheet();
                console.log(sheet);
            }, 3000); 

        },
        
        
    },
    mounted () {
        window.addEventListener('load', () => {
            this.getUnderlyingData();
        })
    }
}

</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...