Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Vue how to display response from backend API

Sorry for such noob question but I'm a simply gave up. I've got below code which creates a POST request to the BE part of the app - user provides input (productCodes) and push submit button. That part works well, Vue sends request to BE but in the response FE should have received JSON with result: { id: 1234 }

How do I get this response and display it inside the app? I've got below:

imports.js

    const createProductsRequest = (self, products) => {
      const jwtToken = self.$store.state.idToken;
      const payload = JSON.stringify({ product_codes: products['product_codes'].split(',') })
    
      return axios
        .post(`/api/v1/imports/products/`, payload,{
          headers: {
            Authorization: `Bearer ${jwtToken}`,
            'Content-Type': 'application/json',
            'Accept': 'application/json'
          }
        })
        .then(response => response.data)
    };

export {
  createProductsRequest
};

sync_product.vue

<script>

import {
  createProductsRequest
} from '../../api/imports'

import ModalController from '../general/modal_controller'

export default {
  name: 'BackboneSyncProducts',
  data() {
    return {
      styleCodes: [],
    }
  },
  computed: {
    productsToSyncAmount () {
      return this.styleCodes.length
    },
  },
  methods: {
    async syncProducts() {
      let confirmationText = `Do you want to ${this.productsToSyncAmount} sync products?`

      if (this.productsToSyncAmount === 0) {
        ModalController.showToast('', 'Type product codes for sync first, please!', 'warning')
      }
      else if (await ModalController.showConfirmation('Confirmation', confirmationText)) {
        try {
          ModalController.showLoader()
          await createProductsRequest(this, this.styleCodes)
          const successMessage = `${this.productsToSyncAmount} products have been queued for sync`
          await ModalController.showToast('', successMessage)
        } catch (data) {
          const errorMessage = `Error occurred during queueing products to sync - `
          ModalController.showToast('', errorMessage + data?.message, 'error')
        } finally {
          this.styleCodes = []
          ModalController.hideLoader()
        }
      }
    },
  }
}
</script>

That's all what I have.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Instead of calling your request without getting the return value:

await createProductsRequest(this, this.styleCodes)

You can get the return value which is the result of the request :

const data = await createProductsRequest(this, this.styleCodes)

By doing that, data must contain the result of the request, as you mentionned { id: 1234 }.

--

If you want to use this result in your component, you can create a reactive value in data()

data() {
  return {
    styleCodes: [],
    data: null
  }
},

And store the result like this :

this.data = await createProductsRequest(this, this.styleCodes)

With that you can display it in your template for example :

<template>
  <!-- There is a v-if because before doing the request, data is null -->
  <div v-if="data">{{ data.id }}</div>
</template>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!