Home > Software design >  Vue.js - how to send send GET request with id as path params
Vue.js - how to send send GET request with id as path params

Time:05-25

I want to send GET request to my backend app and pass ID as a query params. The path I want to use is - GET /api/v1/imports/products_batches/:id. Here is my code:

imports.js

const fetchSyncedProductsResultRequest = (token, id) => {
  return axios
    .get(`/api/v1/imports/products_batches`, {
      params: { id: id },
      headers: {
        Authorization: `Bearer ${token}`,
      }
    })
    .then(response => {
      return response.data['result']
    })
};

sync_products.vue

<template>
  <div>
    <div >
      <button
        type="button"
        
        @click="syncProducts"
      >
        Sync
      </button>
    </div>
  </div>
</template>

<script>

import {
  fetchSyncedProductsResultRequest,
} from '../../api/imports'

export default {
  name: 'BackboneSyncProducts',
  data() {
    return {
      fetchedProductSyncResult: [],
    }
  },
  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 fetchSyncedProductsResultRequest(this, '43').then(data => {
            console.log(data)
            this.fetchedProductSyncResult = data
          })
          // await createApparelMagicProductsRequest(this, this.styleCodes).then(data => {
            // this.loadId = data['id']
          // })
          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>

As you see, I've hardcoded id=43 and added console.log here:

          await fetchSyncedProductsResultRequest(this, '43').then(data => {
            console.log(data)
            this.fetchedProductSyncResult = data
          })

and it returns me undefined.

I don't know, am I sending the query params not correctly or am I have a typo somewhere? How to send this request correctly ?

CodePudding user response:

Replace your

axios
    .get(`/api/v1/imports/products_batches`, ...

with

axios
    .get(`/api/v1/imports/products_batches/${id}`, ...
  • Related