vue.js-如何用ID作为路径参数发送发送请求发送请求

发布于 2025-01-31 11:48:20 字数 2586 浏览 0 评论 0原文

我想将获取请求发送到我的后端应用程序,并将ID作为查询参数传递。我要使用的路径是 - get/api/v1/imports/products_batches/:id。这是我的代码:

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 class="col-12 col-md-3">
      <button
        type="button"
        class="btn btn-primary"
        @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>

如您所见,我已经硬编码id = 43并添加了console。日志在此处:

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

它返回我未定义的

我不知道,我是在发送查询参数不正确还是在某处有错字?如何正确发送此请求?

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 class="col-12 col-md-3">
      <button
        type="button"
        class="btn btn-primary"
        @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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2025-02-07 11:48:20

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

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

Replace your

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

with

axios
    .get(`/api/v1/imports/products_batches/${id}`, ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文