question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property '$on' of undefined

See original GitHub issue

I’m having a hard time catching tree events. I followed your docs (https://amsik.github.io/liquor-tree/#Events) but somehow didn’t work for me and returned an error that says “Cannot read property ‘$on’ of undefined” at this line:

this.$refs.tree.$on(e.name, this.someMethod(e))

Really appreciate your help. 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
become-ironcommented, Feb 19, 2020

@breekoy Or you could just create the tree only after obtaining of data:

<tree v-if="data.length" :data="tree_data" @node:selected="handleNodeSelect"></tree>
0reactions
breekoycommented, Feb 19, 2020

the problem is that the mounted function is invoked before the tree loaded

I guess in the vue, the tree is load in aync way, not sync !

Yes, this is what apparently is happening. In my case, I have 300+ objects coming from an API to be loaded to the tree component. I had the API call on the created() hook thinking that it would be fine. However, when the mounted() hook is invoked, tree is still loading. My guesses would be the API didn’t respond yet when mounted() is invoked or the objects to be loaded are huge enough to cause delay in mounting/loading the tree component.

I somehow made this work by creating an async method that calls the API and call that method inside the mounted() hook with .then() chained into it. Inside the function parameter of then(), I called the this.$refs.tree.setModel() and this.$refs.tree.$on().

Like this:

<tree id="tree" ref="tree" :data="tree_data"></tree>

<script>
    import Tree from 'liquor-tree'

    export default {
        components: { Tree },
        data () {
            return { tree_data: [] }
        },
        methods: {
            async getObjectsFromApi () {
                let response = await someApiCallMethod()
                return response.data
            }
        },
        mounted () {
            this.getObjectsFromApi().then(tree_data => {
                  this.tree_data = tree_data
                  this.$refs.tree.setModel(this.tree_data)
                  this.$refs.tree.$on('node:selected', event => {
                      //some code here to handle the event...
                  })
            })
        },
        beforeDestroy () {
            //making sure tree_data is empty before leaving the page
            this.tree_data = []
            this.$refs.tree.setModel(this.tree_data)
        }
    }
</script>

sorry for a very late response, been kinda busy lately.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot Read Property of Undefined in JavaScript - Rollbar
What Causes TypeError: Cannot Read Property of Undefined. Undefined means that a variable has been declared but has not been assigned a value....
Read more >
Uncaught TypeError: Cannot read property of undefined In
JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or...
Read more >
[Solved] Cannot read Properties of Undefined in JavaScript
The "Cannot read properties of undefined" error occurs when you try to access a property or a method on a variable that stores...
Read more >
Node.js Cannot read property 'on' of undefined - Stack Overflow
My server.js code : var events = require('events').EventEmitter; var v = function() ...
Read more >
Fix: “Cannot Read Property of Undefined” Error in JavaScript
To fix the “cannot read property of undefined” error, use the optional chaining operator on the variable before accessing a property.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found