Troubles with retrieving API search results using Vue computed properties

I've recently developed an Anime Search App using Vue.js and the new script setup. I'm currently struggling to access the search results in order to display the number of titles found. The app is fetching data from an external API, but it's displaying a fixed number for each result instead of the actual titles. Can someone guide me on how to correctly output the number of anime titles? I have limited experience with Vue and JavaScript overall, so any help would be greatly appreciated! Thank you!

Below is the code snippet for my search page:

 <template>
    <header>
            <div class="img-scroll img">
      <h1>Anime Search</h1>
      </div>
    </header>
            <main class="animeSearch">
            <form class="search-box" @submit.prevent="HandleSearch">
                    <input 
                        type="search" 
                        class="search-field" 
                        placeholder="Search for an anime..."
                        required
                        v-model="search_query" />
                </form>
                <p>{{ resultCount }} results!</p>
                <Swiper :slides-per-view="4" :space-between="50" class="cards" v- 
      if="animelist.length > 0">
                <SwiperSlide class="card" v-for="anime in animelist" 
                        :key="anime.mal_id"
                        :anime="anime">
                    
            <a :href="anime.url" target="_blank">
                <img 
                    :src="anime.image_url" 
                    :alt="anime.title + ' Poster'" 
                />
                <h3>{{ anime.title }}</h3>
            </a>
        </SwiperSlide>
                </Swiper>
                <div class="no-results" v-else>
                    <h3>Type in an Anime and hit 'enter' for search results.<br>
                    Use your cursor to swipe through images.<br>
                    Click on the image for information about the anime.<br>
                    Happy searching!...</h3>
                </div>
            </main>
             <footer>
      <p>&copy Megan Louise 2022</p>
      </footer>
    </template>

    <script>
    import { ref } from 'vue';
    import { Swiper, SwiperSlide } from "swiper/vue";
    import { computed } from "vue";
    import "swiper/css";
    export default {
      components: {
        Swiper,
        SwiperSlide,
      },
        setup() {
            const onSwiper = (swiper) => {
            console.log(swiper);
            };
            const search_query = ref("");
            const animelist = ref([]);
            const HandleSearch = async () => {
                animelist.value = await fetch(`https://api.jikan.moe/v3/search/anime? 
      q=${search_query.value}`)
                    .then(res => res.json())
                    .then(data => data.results);
                search_query.value = "";
            };
            const onSlideChange = () => {
            console.log('slide change');
            };
            const resultCount = computed(() => {
                return animelist.value.length;
                console.log('resultCount');
        });
            return {
                search_query,
                animelist,
                HandleSearch,
                onSwiper,
                resultCount,
                onSlideChange
            };
        },
    }
    </script>

    <style scoped>
    input[type='search'] {
    font-family: 'BIZ UDGothic', sans-serif;
    background-color: black;
    border: 2px solid #FD8721;
    color: #FD8721;
    width: 100%;
    height: 40px;
    padding: 5px;
    max-width: 600px;
    border-radius: 8px;
    margin-bottom: 40px;
    font-size: 22px;
    }
    h3 {
    color: #ECC013;
    text-shadow: 1px 1px #FD8721;
    font-weight: 400;
    }
    .cards {
        display: flex;
        flex-wrap: wrap;
        margin: 10px 40px;
        justify-content: space-evenly;
        cursor: pointer;
        }
    
    .card {
    width: 100%;
    max-width: 200px;
    height: auto;
    padding: 0px 8px;
    margin-bottom: 16px;
    }
    img {
        cursor: grab;
        width: 100%;
        height: 300px;
        object-fit: cover;
        border-radius: 16px;
        box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.15);
        transition: 0.4s;
        border: 2px solid #BA1039;
    }
    img:hover {
            transform: scale(1.05);
        }
    @media only screen and (max-width: 820px) { 
    h1 {
    font-size: 3em;
    }
    }
    </style>

Answer №1

As outlined in the Jikan API documentation, starting from March 1st 2022, version 3 of the API will be deprecated - https://docs.google.com/document/d/172RQ9wWiXqOnGqjXrV3cxMNceiqwCjxjprSFuyLwQJM/edit

Prior to September 1st 2022, you are required to transition to version 4 before version 3 is no longer supported. The version 4 documentation specifies that your API request should now be modified to

The response will provide details regarding pagination such as current page, number of items on the page, if it is the last page, and the total results:

{
  "pagination":
  {
    "last_visible_page":1,
    "has_next_page":false,
    "current_page":1,
    "items":
    {
      "count":20,
      "total":20,
      "per_page":25
    }
  },
  data: [....]
}

The data key will contain the items specific to the current page

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a TypeScript error in Vue 3 when trying to assign a type of '($event: any) => void' to an event listener

Snippet of component code: <h2 @click="handleEvent(post.id)">{{ post.title }}</h2> function handleEvent(id: number) { router.push("/post/" + id); } Error message in TypeScript: Type '($event: any) => void' i ...

Determine if a file input is linked in React.js Material UI

Currently, I am working with a Material UI <TextField /> component that has a type=file prop to allow file attachments. My goal is to trigger an event when a file is attached to this TextField. However, my search results only provided JQuery soluti ...

The model.find operation is failing to retrieve the necessary fields from the database

When I execute console.log(correct.password), it returns undefined, even though the if condition results in false. app.post('/login' , async (req , res)=> { const correct = data.findOne({name : req.body.name}).select({name : 0}); if(!c ...

Managing OAuth2 redirections on the frontend: Best practices

I am currently working on implementing an OAuth2 flow for a Single Page Webapp, but I am facing challenges in dealing with Frontend/JavaScript redirects. Regarding the backend setup, I have it all sorted out: utilizing a library that takes care of everyth ...

Combining two input text strings

A React component I'm working on takes the user's first and last name to create unique usernames for them. class App extends Component { render() { return ( <div className="App"> First name:<br/> <input ...

Finding out which carousel the user is currently engaging with in the Slick Carousel can be accomplished by following these steps

I am struggling with applying a class to the carousel container when the first slide is active in a page that contains multiple Slick carousels. Is there a way to update the code below so that the class is only applied to the specific carousel the user is ...

Personalize JSON Reporter to Display Captured Information - WebdriverIO

I'm currently in the process of scraping data from an android app using WDIO and APPIUM. After successfully storing the scraped data in an array, I now aim to automatically output this array data to a .json file, rather than manually copying and pasti ...

A step-by-step guide on crafting a customized archetype for nodeJs projects

Exploring the world of the node.js framework and looking to develop a template blueprint for node.js projects. The aim is to establish a set structure with essential folders and configuration files so that others can easily generate their own projects usin ...

Integrating AngularJS code into dynamically generated HTML using JavaScript

I am currently utilizing an outdated version of AngularJS (1.3). I have a page where I need to dynamically display different content based on database values. If the user interacts with the page and changes the database value, I want the displayed content ...

When using Lockdown.js with NPM, I encounter a blank file being returned

When using Lockdown.js for NPM, I encountered an issue where the command to generate the lockdown file resulted in an empty file. Here are the links for the dependencies: NPM lockdown git Here is a snippet from my package.json: { "name": "nw", "pri ...

Several JavaScript functions require a confirmation dialog to be displayed before they can be executed

There are three separate javascript/jquery functions in my code, all triggered by a button click. One function posts to a form handler, another creates a new tab, and the third one sends new data into the tab through an ajax call. These functions are inter ...

There seems to be a connection issue between my Jquery and HTML, as they

I've hit a roadblock because my jQuery isn't connecting and I can't seem to figure out why. It's been stumping me for hours. Here is the HTML code snippet from exercise6.html: <!DOCTYPE html> <html lang="en> <h ...

Utilizing Jquery to create a carousel with looping functionality for flowing text

I am currently facing an issue with a carousel that contains multiple images and text. In order to make the text responsive, I have implemented a script called FlowText. The script works perfectly on the initial carousel image (the active one), but as soon ...

JavaScript has received an event on Server XHR

Situation: There is a scenario where the target API is external and cannot be altered. A JS client initiates sending data to the API upon clicking a button. The code snippet resembles something like this: $.ajax({ type: "POST", url: &quo ...

The response from the ajax call is still pending

I am currently working on integrating MySQL data (select query) using Spring and MyBatis. I have a controller function that is called via ajax in JavaScript to retrieve the database data. For example: ajax url: /testmysql controller requestmapp ...

The Nuxt3 application is experiencing technical issues when running in production

My app performs well in development mode when using "npm run dev". However, once I build it with "npm run build" and then launch it with "npm run start", it starts to malfunction. Specifically, the dynamic styles that control whether a button should be d ...

Tips for utilizing the json_encode function with an array

I am trying to extract specific data from an object created using the json_encode function in PHP. while($locations=$req->fetch()){ $t = $locations->titre; $la = $locations->latitude; $lo = $locations->longitude; $typ = $lo ...

What is the best way to pass route props using the <router-link> component?

Check out the button below that links to a specific route: <router-link class="q-pa-md" :to="{ name: 'Edit'}" id="item.id"> <q-btn outline>Edit</q-btn> </router-link> Take a look at my router se ...

State is not currently utilizing the variable

const DonorsTables = () =>{ const [search, setSearch] = useState(""); const [countries, setCountries] = useState([]); const [filteredcountries, setFilteredCountries] = useState([]); const getCountries = async () => { try { ...

Exploring the keyof operator in Typescript for object types

Is there a way to extract keys of type A and transfer them to type B? Even though I anticipate type B to be "x", it seems to also include "undefined". Why does the keyof operator incorporate undefined in the resulting type? It's perplexing. I kn ...