29

I'm trying to make async autocomplete input with Vue, Nuxt, Axios and Buefy. It basically works, but I need to have different strings when user just starts typing and there's yet nothing to show, and when there is nothing found for such request.

I'm checking in computed variable if input value isn't empty and axios returns empty array to handle if the request address cannot be found. But it causes error

Cannot read property 'length' of undefined

The weird thing is that address variable is successfully used in other parts of my component.

My vue file below:

<template lang="pug">
b-field(label="Your address?")
    b-autocomplete(
    rounded,
    v-model="address",
    :data="data",
    placeholder="Start typing",
    icon="magnify",
    @input="getAsyncData",
    @select="option => selected = option",
    :loading="isFetching"
    )
        template(slot="empty") {{ dummyText }}
</template>

<script>
import axios from 'axios'
import debounce from 'lodash/debounce'

export default {
    data() {
        return {
            data: [],
            address: '',
            selected: null,
            isFetching: false,
            nothingFound: false,
            test: false
        }
    },

    computed: {
        dummyText: () => {
            if (this.address.length > 0 && this.nothingFound) { // This will return error
                return 'There is no such address'
            } else {
                return 'Keep typing'
            }
        }
    },

    methods: {
        getAsyncData: debounce(function () {
            this.isFetching = true

            axios.post('https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address', {
                "query": this.address,
                "count": 8
            }, {
                headers: {
                    'Authorization': 'Token sometoken',
                    'Content-Type': 'application/json',
                    'Accept': 'application/json',
                }
            })
                .then(response => {
                    this.isFetching = false
                    this.data = Object.values(response.data.suggestions)
                    if (response.data.suggestions.length===0) this.nothingFound = true
                    console.log(this.address.length) // This will work
                })
                .catch(error => {
                    this.isFetching = false
                    console.log(error);
                })
        }, 300)
    }
}
</script>

This is not about ssr, I've tried to init component inside mounted hook. Think I'm missing out something obvious, but I've already spent hours trying to fix this without success

2
  • Are you allowed to have a data field named 'data'? Sounds like you might be accidentally clearing wiping out other data fields on accident, which is why address is undefined until it is set with the v-model? Commented May 9, 2018 at 19:00
  • @AaronPool Just tried renaming 'data', no effect Commented May 9, 2018 at 19:05

3 Answers 3

74

Don't use arrow function ()=>{} for computed, it will cause the wrong context (not current Vue instance).

Change to function () {} then it should work fine.

And for methods, watch, you should follow same rules.

computed: {
    dummyText: function () { // change to function () {}
        if (this.address.length > 0 && this.nothingFound) { // This will return error
            return 'There is no such address'
        } else {
            return 'Keep typing'
        }
    }
},
Sign up to request clarification or add additional context in comments.

Comments

13

You can also use es2015 shorthand for a method function:

computed: {
    dummyText() {
        return this.address.length > 0 && this.nothingFound ? 'There is no such address' : 'Keep typing';
    }
}

1 Comment

Eslint expects this: Expected method shorthand
5

The Vue Documentation states not to use arrow functions on a property or callback.

You are facing this error because an arrow function wouldn't bind this to the vue instance for which you are defining the computed property as arrow functions are bound to the parent context and this.address is undefined. Same would happen if you use arrow function for methods.

Use regular function:

dummyText: function () {
    console.log(this.address)
}

Or use ES5 shorthand:

dummyText() {
    console.log(this.address)
}

Or If you want to keep using the arrow function, you could pass the component instance (this) as parameter because computed properties receive component instance as their first argument. :

dummyText : ctx => console.log(ctx.address)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.