Search code examples
javascriptfirebasevue.jsvuefire

How to create dynamic references in VueFire?


I'm trying to create refs dynamically:

My first ref works because it's hardcoded but the second doesn't, apparently because it's dynamic:

 firebase: function(){
    return {
        categories: db.ref('categories'),
        subcategories: db.ref('categories').child(this.addItem.category)
    }   
 }

Does anyone know how to get around this?


Solution

  • I created a dynamic db ref by using vm.$bindAsObject found in the vue-fire docs. I used it in the created hook with a project id, like so:

    created () {
      this.$bindAsObject('project', db.ref('projects/' + this.$route.params.id))
    }
    

    If you've already got a db.ref binding you might have to vm.$unbind but I haven't had to do that yet.