1

I have two API get methods. The first is to check if the user is an admin. The second is to call all the user lists.

But I am having a hard time calling first; It always calls all the user lists before I check if the user is admin or not.

What I did was

 async mounted() {
            await this.getCurrentUser();
        }

and method to check if user is admin:

getCurrentUser(){
                Common.getUser().then(data => {
                    if(!data.admin){
                        this.isAdmin = false;
                    }
                });
            },

method to get all users:

paging: function (pageNumber) {

                vm.requestParam.page = pageNumber == undefined ? 
                
                var param = Common.param(vm.requestParam);

                axios.get('/api/users?' + param).then(function (response) {
                  ///

                    }                       

                })
                    .catch(function (error) {
                        console.log(error.response);
                    });
            },

and the data is:

data: {
            resources: {},
         
           'requestParam': {
                'id': '',
                'query': '',
                'accessLevel': '',
                'page': 1
            },`

1 Answer 1

1

The problem is that promise chains are broken, so await has no effect. As a rule of thumb, a function that involves promises should return a promise that represents the end of its job. It should be:

return Common.getCurrentUser()...

and

return axios.get('/api/users?' + param)...

Callback-based nextTick can result in race condition. In case it affects other methods, it should be a promise, too:

await this.$nextTick();
vm.getResources();
...
Sign up to request clarification or add additional context in comments.

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.