Объект Json не обновляется в другой функции

Я работаю над Laravel и Vuejs, я вызываю API из своего vue js usign Axios и получаю возвращаемые данные в объекте. Но этот объект не предоставляет или не получает никаких данных о следующем методе на той же странице vue.

Моя первая функция:

getCompanyPaymentMethodLists: function(){
            let that = this;
            axios.get('/api/payment_method/get_company_payment_method')
                .then((response) => {
                that.company_payment_method_lists = response.data;
                /* Generate sale_paids object */
                if(that.company_payment_method_lists){
                  var cash_payment_method_id = that.company_payment_method_lists.filter(function (payment_method) {
                        return payment_method.name == "Cash";
                  });
                  /* For Cash Payment*/                      
                  that.customer_order_details.cash_payment_method_id = cash_payment_method_id[0].id;
                } 
            })
            .catch(function (error) {
                that.errors = error;
            });
        },

И моя вторая функция: в этой функции я хочу получить свойство объекта that.customer_order_details.cash_payment_method_id, но оно дает значение null

getSalePaidDetail:  function(){
          let that = this;
          console.log('working' + that.customer_order_details.cash_payment_method_id); // Print Null
          axios.post('/api/sale_paids/get_sale_paid_history', {
              sale_id: null,
              cash_payment_method_id: that.customer_order_details.cash_payment_method_id
          })
          .then((sale_paid) => {                 
              that.customer_order_details.sale_paids = sale_paid.data;
          })
          .catch(function (error) {
              that.errors = error;
          });  
        },

Здесь that.customer_order_details определяется как объект:

data(){
        return{ 
              customer_order_details:{                  
              cash_payment_method_id: null,
        }
     }

person leaveme_alone    schedule 05.07.2020    source источник
comment
Как первая функция вызывает вторую функцию?   -  person LLai    schedule 06.07.2020
comment
вам может потребоваться вызвать первую функцию внутри второй функции и дождаться ее. используйте async/await method или promise   -  person Karl L    schedule 06.07.2020
comment
Разве нельзя разделить обе функции? и не могли бы вы показать мне пример метода async / wait?   -  person leaveme_alone    schedule 06.07.2020


Ответы (1)


У меня есть решение, спасибо за ваши усилия.

async mounted() {
        let that = this;               
        await that.getCompanyPaymentMethodLists(); 
        await that.getSalePaidDetail(); 
    },

Затем в сторону:

methods: {  
getCompanyPaymentMethodLists: function(){
            let that = this;
            return axios.get('/api/payment_method/get_company_payment_method')
                .then((response) => {
                that.company_payment_method_lists = response.data;
                /* Generate sale_paids object */
                if(that.company_payment_method_lists){
                  var cash_payment_method_id = that.company_payment_method_lists.filter(function (payment_method) {
                        return payment_method.name == "Cash";
                  });
                  /* For Cash Payment*/                      
                  that.customer_order_details.cash_payment_method_id = cash_payment_method_id[0].id;
                } 
            })
            .catch(function (error) {
                that.errors = error;
            });
        },
getSalePaidDetail:  function(){
          let that = this;
          console.log('Heda working' + that.customer_order_details.cash_payment_method_id);

          axios.post('/api/sale_paids/get_sale_paid_history', {
              sale_id: null,
              cash_payment_method_id: that.customer_order_details.cash_payment_method_id
          })
          .then((sale_paid) => {
              that.customer_order_details.sale_paids = sale_paid.data;
          })
          .catch(function (error) {
              that.errors = error;
          });  
        },

}
person leaveme_alone    schedule 08.07.2020