Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eggplantSword's avatar

Axios get, can't get it to return data

I feel like I'm missing something but I can't get this to work at all, what I'm doing is: getting an array of ids, when calling the name I'm making a axios get to retrieve the item and return the name, seems simple enough.

This is my code

<div v-if="active_filters.length > 0">
    <a v-for="active in active_filters">
        <span>
            {{ getActive(active) }}
        </span>
    </a>
</div>

data() {
    return {
        active_filters: [],
    }
},
methods: {
    async getActive(val) {
        console.log('await');
        console.log(await this.catName(val)); //returns correct data
        return await this.catName(val); // returns [object Promise] instead
    },
    catName(val) {
        return axios.get(`${url_api}` + 'admin/categories/' + val)
            .then(function (resp) {
                return resp.data.category
            })
    },
}

I have no idea why this isn't working, I've tried at least 5 different ways and no matter what I do it always returns [object Promise] .

What am I doing wrong?

0 likes
4 replies
tykus's avatar

You could do the following instead:

    async catName(val) {
		let category;
        await axios.get(`${url_api}` + 'admin/categories/' + val)
            .then((resp) => category =  resp.data.category)
		return category;
    },

I am wondering why you need an axios request for every active_filter; is there a better way to fetch all of the data you need using fewer requests?

eggplantSword's avatar

I gave up using axios, and instead made a call in the mounted to get all the categories and then I did a map to find the correct name

getActive(val) {
    var category = null;
    this.categories_all.map(cat => {
        if (cat.id === val) {
            category = cat.category
        }
    });
    return category;
},
tykus's avatar

Better that there is only one XHR request at least!

eggplantSword's avatar

yea, I guess that's true, and it's better for me since I'd been trying to get the axios to work for an embarrassingly long time and could never get it to work.

Please or to participate in this conversation.