Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue js ajax inside another ajax is getting data but not rendering view

There is a situation that I have to get extra data after my first ajax (in mounted function) in vuejs, I have put the second ajax in if condition and inside success function of the first ajax!

It is working and I see data in Vue Devtools in chrome, but data is not rendered in view.

Pseudo Code:

var vm = new Vue({
         el: '#messages',
        data: {
            participants: [],
            active_conversation: '',
            messages: []
        },

        methods: {

            getParticipants: function () {
                   return this.$http.post('message/get-participants').then(
                    function (response) {
                      
                        vm.participants = response.data.participants;
                        // if there is a conversation_id param in url
                        if (getUrlParameterByName('conversation_id')) {
                             // Second Ajax Is Called Here inside First Ajax
                             return vm.getConversationMessages (getUrlParameterByName('conversation_id')); // this ajax call is getting data but not showing in view  
                        }
                    }

            },
       
           getConversationMessages : function(conv_id){
              // Second Ajax Call to get Conversation messages
              // and showing them , works onClick
               return this.$http.post('message/get-messages/' + conv_id).then(
                    function (response) {
                        if (response.data.status == 'success') {
                            console.log(response.data.messages)
                            vm.messages = response.data.messages;
                            vm.$forceUpdate();
           }
        },


      mounted: function () {
            this.getParticipants()
        }

})

The Second Ajax Call to get a specific conversation messages is responding to onclick event and showing messages, but when this function is used inside the First Ajax success response (getParticipants()), its getting data correctly nd I can see in DevTools VueJs Extension that messages are set but view does not show messages, I have tried vm.$set() but no chance.

Update:

The second Ajax is working with no errors and messages data property get filled (I checked Vue DevTools), The only problem is that view does not show the messages!! but when I do it manually by clicking on a conversation, second ajax is executed again and I can see messages!, I also tried vm.$forceUpdate() after second ajax with no chance.

Update2 html part(the bug is here!!)

<a vbind:id="conv.id" v-on:click="getMessages(conv.id)" onclick="$('#user-messages').addClass('active')">
like image 366
Ahmad Mobaraki Avatar asked Nov 21 '17 18:11

Ahmad Mobaraki


2 Answers

the DOM is updated with messages with when you do the ajax request with only getConversationMessages and not placing getConversationMessages in the success callback of the ajax request of getParticipants is the fact that an error is encountered at this line

this.participants = response.data.participants;

you are using a normal function in the success callback of the ajax request that's the reason this does not point to the vue instance adnd this.participants gives you an undefined error. So use vm insteaad to point to the vue instance as you did in the rest of the program

vm.participants = response.data.participants;

Edit

var vm = new Vue({
         el: '#messages',
        data: {
            participants: [],
            active_conversation: '',
            messages: []
        },

        methods: {

            getParticipants: function () {
                 return this.$http.post('message/get-participants');
            },

           getConversationMessages : function(conv_id){
               return this.$http.post('message/get-messages/' + conv_id);
           }
        },


      mounted: function () {
            this.getParticipants().then(function (response){

                vm.participants = response.data.participants;

                if (getUrlParameterByName('conversation_id')) {
                    return vm.getConversationMessages (getUrlParameterByName('conversation_id')); // this ajax call is getting data but not showing in view  
                }
            }).then(function(response){
                if (response.data.status == 'success') {
                console.log(response.data.messages)
                   vm.messages = response.data.messages;
            });

        }

})
like image 147
Vamsi Krishna Avatar answered Sep 22 '22 01:09

Vamsi Krishna


Call second http request after first is completed using http callback or you can use Promise too.

return this.$http.post(function(response){

   // first call
}).then(function(response){

// Second call
})
like image 25
gaurav bankoti Avatar answered Sep 18 '22 01:09

gaurav bankoti