adding an item to items by push before axios post does not send the item added

1

Good morning, thanks in advance for the help you could give me. I'm creating a page for issuing an invoice in laravel with vue and axios. Before sending the invoice to record, I must verify if the total value of the invoice makes you a winner, that if so, it must be added to the detail of the invoice with zero cost (the detail is in items []). If the prize is obtained, the prize will be charged to the items and sent to record with laravel with axios post as you can see in the code. The problem I have is that laravel does not reach the line of the prize that was loaded at the end, only those who entered the page arrive, however on the page itself if you can see that the prize was charged, since immediately shows that I charge it with this.items.push ().

Why do I have this problem, maybe the axios.post does not wait for me to complete the calculate_premium method ?, I have to pause before doing the axios.post (), I do not know what to do, I need your help please.

Thank you.

...
data: {
...
items: []
},
...
calcular_premio: function() {

   if(cumple_condicion) {

       this.items.push({});

   }

},

grabar_factura: function() {

   //antes de grabar la factura verificamos si se hace acreeedor a un premio
   this.calcular_premio();
   //aqui mandamos a grabar mediante una ruta de laravel
   axios.post('/ventas/grabar_venta_ventanilla', ...

},

    
asked by Marco Ruiz 21.07.2018 в 13:59
source

1 answer

1

I found the solution. In calculating prize I use axios.get to in laravel do the calculation of the prize. Only when I receive an answer, should I make the recording with axios.post. Axios.get runs and the code continues its flow without waiting for an answer. Something like this:

calcular_premio: function() {
   axios.get('venta/calcular_premio')
         .then(response => {
            if (response.data.success) {
               this.items.push({});
            }
            axios.post('venta/grabar', ...);
          });  
   //el código siguiente se ejecuta inmediatamente después de axios.get
   //por lo que la grabación debe estar dentro, cuando se reciba respuesta
   ...


}
    
answered by 21.07.2018 в 14:37