Get data from api before rendering component


Alex

I send 2 api requests before rendering the page:

const Profile = {
    template: '#profile',
    attributes: null,
    photos: [],
    data: function () {
        return {attributes: Profile.attributes, photos: Profile.photos};
    },
    beforeRouteEnter: function (to, from, next) {
        function getProfile() {
            return axios.get('user/get-profile?access-token=1', {responseType: 'json'});
        }
        function getPhotos() {
            return axios.get('photos?access-token=1', {responseType: 'json'});
        }

        axios.all([getProfile(), getPhotos()])
            .then(axios.spread(function (profile, photos ) {
                console.log(profile, photos );
                next(vm => {
                    vm.setProfile(profile);
                    vm.setPhotos(photos);
                })
            }));
    },
    methods: {
        setProfile: function (response) {
            Profile.attributes = response.data;
            console.log(Profile.attributes);
        },
        setPhotos: function (response) {
            Profile.photos = response.data;
            console.log(response);
        },           
    }
};

The problem is that rendering happens before setProfileand setPhotosmethod . How can I render my component correctly?

Phil

Try using async/await. I have removed beforeRouteEnterand axios.spreadadded create. After all requests are made, the component will be loaded.

const Profile = {
    template: '#profile',
    attributes: null,
    photos: [],
    data() {
        return {
            attributes: null,
            photos: null
        };
    },
    async created() {
        const getProfile = await axios.get('user/get-profile?access-token=1', {
            responseType: 'json'
        });
        const getPhotos = await axios.get('photos?access-token=1', {
            responseType: 'json'
        });

        this.setProfile(profile);
        this.setPhotos(photos);
    },
    methods: {
        setProfile(response) {
            this.attributes = response.data;
            console.log(this.attributes);
        },
        setPhotos(response) {
            this.photos = response.data;
            console.log(response);
        }
    }
};

Shorter

const Profile = {
    template: '#profile',
    attributes: null,
    photos: [],
    data() {
        return {
            attributes: null,
            photos: null
        };
    },
    async created() {
        this.attributes = await axios.get('user/get-profile?access-token=1', {
            responseType: 'json'
        });
        this.photo = await axios.get('photos?access-token=1', {
            responseType: 'json'
        });
    }
};

Related


Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from server before rendering component in ReactJs

Damodaran I am new to this and I am trying to get data from a server and pass it to another component to list it. But the other component is rendered before the data is fetched from the server. So I get Uncaught TypeError: datalist.maps is not a function. How

Get a component to wait for async data before rendering

Countless Bruts I'm calling an endpoint to bring back an object that does get the data, but the object isn't fast enough for the component to get the data and render. Instead, the component will render with blank values where there should be data. If I break t

Get a component to wait for async data before rendering

Countless Bruts I'm calling an endpoint to bring back an object that does get the data, but the object isn't fast enough for the component to get the data and render. Instead, the component will render with blank values where there should be data. If I break t

Get a component to wait for async data before rendering

Countless Bruts I'm calling an endpoint to bring back an object that does get the data, but the object is not fast enough for the component to get the data and render. Instead, the component will render with blank values where there should be data. If I break

How to get child component after adding data before rendering?

cabbage I want to set some props of child component after adding data, but how can I do that? In the following example, I want to select the item added in the methods additem(), how to do it in Vue? Vue.component('list-item', { props: ['text'], data()

How to get child component after adding data before rendering?

cabbage I want to set some props of child component after adding data, but how can I do that? In the following example, I want to select the item added in the methods additem(), how to do it in Vue? Vue.component('list-item', { props: ['text'], data()

How to get child component after adding data before rendering?

cabbage I want to set some props of child component after adding data, but how can I do that? In the following example, I want to select the item added in the methods additem(), how to do it in Vue? Vue.component('list-item', { props: ['text'], data()

React hook component rendering before API call

Tamás Dobi: I need to create a React app that lets you list Pokémon and types. I am getting data from PokeAPI. Is it good practice to get it from the App component and then pass it to the child component, or is it better to get them from the child component? I

React hook component rendering before API call

Tamás Dobi: I need to create a React app that lets you list Pokémon and types. I am getting data from PokeAPI. Is it good practice to get it from the App component and then pass it to the child component, or is it better to get them from the child component? I

React hook component rendering before API call

Tamás Dobi: I need to create a React app that lets you list Pokémon and types. I am getting data from PokeAPI. Is it good practice to get it from the App component and then pass it to the child component, or is it better to get them from the child component? I

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't

How to stop component rendering before getting data?

kill Currently, in React, I have to use a function in the componentDidMount lifecycle method, where the action creator is called to fetch the data into it. However, the component will use the data, depending on the speed of the response, to determine if the da

Set the data fetched by ajax to the component before rendering

Slabo I use Vue Router with file based components. I call one of the routes CaseDetail.vueand it gets a parameter (slug) which gets json from an API with axios. I'm doing this using " in-component guarding " , the method is called beforeRouteEnter. I'm passing

How to stop component rendering before getting data?

kill Currently, in React, I have to use a function in the componentDidMount lifecycle method, where the action creator is called to fetch the data into it. However, the component will use the data, depending on the speed of the response, to determine if the da

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't

How to stop component rendering before getting data?

kill Currently, in React, I have to use a function in the componentDidMount lifecycle method, where the action creator is called to fetch the data into it. However, the component will use the data, depending on the speed of the response, to determine if the da

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't

Finish rendering component before fetching data

Faris I am trying to get data by creating a function. In that function, I'm trying to set the state, and call the state from the componentDidMountmethod , but I'm having some problems: I'm not sure if it's whilegood practice because I'm looping and changing en

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't

ReactJS - load data before rendering component

Adel In ReactJS I change the route to a new route with "id" and then based on this "id" I have to call the API and get the data. I used an API call componentDidMountand componentWillMounttried setState()to keep the data in that state. But in my case they don't