Home > Software engineering >  Uncaught TypeError: Cannot set properties of undefined (setting 'isSubcon')
Uncaught TypeError: Cannot set properties of undefined (setting 'isSubcon')

Time:11-23

need some help here.

I want my navigation drawer to display different contents based on the user types/roles of those who logged in.

But I encounter this error ("Uncaught TypeError: Cannot set properties of undefined (setting 'isSubcon'"). and my navigation drawer not displaying anything.

my template

<v-list v-if="isSubcon">
    //content of drawer if user that logged in is a "Subcon"
</v-list>

<v-list v-if="isWPC">
    //content of drawer if user that logged in is a "WPC"
</v-list>

<v-list v-if="isBWG">
    //content of drawer if user that logged in is a "BWG"
</v-list>

my script

data: () => ({
    isSubcon: false,
    isWPC: false,
    isBWG: false,
}),

// I think below is where the problem occurs.
created() {
    firebase
        .auth()
        .onAuthStateChanged((userAuth) => {
            if (userAuth) {
                firebase
                    .auth()
                    .currentUser.getIdTokenResult()
                    .then(function ({
                            claims,
                        }) {
                            if (claims.customer) {
                                this.isSubcon = true;  //HERE WHERE THE PROBLEM OCCURS (i think)
                            } else if (claims.admin) {
                                this.isWPC =true;  //THIS ALSO
                            } else if (claims.subscriber) {
                                this.isBWG = true;  //AND THIS ALSO
                            }
                           }
                        )
                }
            });
    },

I'm using Firebase Custom Claims to log in and Nuxt (mode: SPA) with Vuetify for the UI. So how can I remove the error, so that I can display the content to my nav drawer?

Thank you in advance :))

CodePudding user response:

this depends always on the scope/object called and both properties data: () => ({ ... }) and created() seem to be properties of another object. If you invoke it with:

myObject.created();

then the solution below should do it.

created() {
  let that = this;
  firebase
   ...
    .then(function ({ claims }) {
      if (claims.customer) {
        that.isSubcon = true;  //HERE WHERE THE PROBLEM OCCURS (i think)
      } else if (claims.admin) {
        that.isWPC =true;  //THIS ALSO
      } else if (claims.subscriber) {
        that.isBWG = true;  //AND THIS ALSO
      }
    })
}
  • Related