Devin Chaves
Devin Chaves

Reputation: 175

Vue: Trouble retrieving cookie and passing into request

  methods: {
getCookie (name) {  /// Retrieve cookie function
  match = document.cookie.match(new RegExp(name + '=([^;]+)'));
  if (match) return match[1];
  return
},
handleLoginFormSubmit () {
  const postData = {
    username: this.login.username,
    password: this.login.password
  }
  const servicegroupid = getCookie("servicegroupid") ///Call cookie function assign to var
  const authUser = {}

  this.$http.post(loginUrl + "/" + servicegroupid, postData, {emulateJSON: true}) /// Pass variable into request
    .then(response => {
      if (response.status === 200) {
        console.log('session', response)
        authUser.auth = response.data.auth
        authUser.sgid = response.data.sgid
        authUser.user = this.login.username
        window.localStorage.setItem('authUser', JSON.stringify(authUser))
        this.$store.dispatch('setUserObject', authUser)
        this.$router.push({name: 'dashboard'})
      }
    })
  }
}

Attempting to retrieve a cookie, assign it to a variable, and pass it into a request. Getting lots of undefined.

Vuex, resource, and router are all working fine.

Getting error:

getCookie is not defined

If I try to pull the function out of the methods block I get

match is not defined

Any help is greatly appreciated.

Upvotes: 4

Views: 8480

Answers (1)

Tomasz Rup
Tomasz Rup

Reputation: 859

Change

const servicegroupid = getCookie("servicegroupid") 

to

const servicegroupid = this.getCookie("servicegroupid")

It's a method of your component.

Upvotes: 2

Related Questions