Difficulty extracting axios.then response



  • I can send an axios.post request without issues within Quasar, and I can get a response. However, other than running console.log or alert within .then(function(response)) I can’t seem to figure out how to get that information outside of .then’s function.

    For instance:

    methods: {
        ...mapActions('auth', ['loginUser']),
        anotherMethod(response) {
            alert('This works!')
        },
        submitForm() {
            axios
            .post('/authenticate.php', {
                rememberme: true,
                username: this.formData.userName,
                password: this.formData.password
            })
            .then(function(response) {
                console.log(response.data) // works just fine
                alert(response.data) // works just fine
                this.anotherMethod(response.data) // doesn't fire alert('This works!')
                this.loginUser(response.data) // doesn't fire in Vuex store 'auth'
                return response.data // I can't find any way to grab this
                /* what can I do here to extract response.data from axios.then? */
            })
            .catch(function(error) {
                console.log("Axios Error: ",error)
            })
        }
    }
    

    Any of the failed attempts simply produce “undefined” errors in console.log. Suggestions?



  • @omgwalt use arrow function. Ie in your then callback response => { ... }.



  • Thank you! That did the trick.


Log in to reply