Home > Software engineering >  Issue with Api call, not returning any response in Vuejs?
Issue with Api call, not returning any response in Vuejs?

Time:02-17

import axios from "axios";

export const routerid = (itemId) =>
  axios.get("https://fakestoreapi.com/products?limit="   itemId);
<template>
  <div>
    <div v-for="(item, key) in user" :key="key">
      {{ item.price }} <br />
      {{ item.description }} <br />
    </div>
  </div>
</template>

<script>
import { routerid } from "./routerid";
export default {
  name: "User",
  components: {},
  data() {
    return {
      lists: [],
    };
  },
  mounted() {
    if (this.$route.params.id)
      routerid(this.$route.params.id).then((r) => {
        let obj = r.data;
        this.lists = [{ ...obj }];
      });
  },
  computed: {
    user: function () {
      return this.lists.filter((item) => {
        return item.id === this.$route.params.id;
      });
    },
  },
};
</script>

How to make axios url call with query params like this..https://fakestoreapi.com/products?limit=1

Did i correctly call the api or anything missing in the code logic. As of now, In my output, I cant see any response from the api.

Code:- https://codesandbox.io/s/cocky-ives-h19zm7?file=/src/components/routerid.js

CodePudding user response:

Don't you want to fetch a product by its ID? Then it rather would be:

export const routerid = async (itemId) =>
  await axios.get("https://fakestoreapi.com/products/"   itemId);

https://codesandbox.io/s/silly-nightingale-ixz8tr

CodePudding user response:

export const routerid = async (itemId) =>
  await axios.get("https://fakestoreapi.com/products?limit="   itemId);

CodePudding user response:

try to do

export const routerid = async (itemId) =>
  await axios.get("https://fakestoreapi.com/products/"   itemId);

Or

async function get()
  await axios.get("https://fakestoreapi.com/products?limit="   itemId);

Or 
  axios.get("https://fakestoreapi.com/products?limit="   itemId).then(res => 
  console.log(res)
);
  • Related