Is it possible to retrieve and display an object from an API using its unique ID?

I created a straightforward application. The main page displays a list of movies fetched using an API, and upon clicking on a particular movie, it leads to a new page with detailed information about that movie. On the details page, another API call is made. I modified the URL so that when the user clicks on 'More Info', it retrieves the ID of the movie object. To achieve this, I wrote a function that extracts the ID from the URL and compares it with the one obtained from the API call. If they match, the function returns true. But now I'm faced with the challenge of how to retrieve and display the movie information based on this data. What approach would you suggest? Here's the code snippet:

<template>
    <div>
        <div v-for="info in movieInfo"
            :key="info.id">

            {{info.id}}
        </div>
    </div>
</template>

<script>
import axios from 'axios'

    export default {
        name: 'ViewComp',
        data() {
            return{
                movieInfo: [],
            }
        },
        mounted () {
            axios
                .get('https://api.themoviedb.org/3/movie/popular?api_key=###&language=it-IT&page=1&include_adult=false&region=IT')
                .then(response => {
                    this.movieInfo = response.data.results
                    // console.log(response.data.results)
                })
                .catch(error => {
                    console.log(error)
                    this.errored = true
                })
                .finally(() => this.loading = false)
        },
        methods: {
            confrontID(){
                var url = window.location.href;
                var idUrl = url.substring(url.lastIndexOf('/') + 1);
                var idMovie = this.info.id;

                if (idUrl === idMovie) {
                    return true;
                }
            }
        }
        

    }

    
</script>

<style scoped lang="scss">
/*Insert component styles here*/
</style>

Answer №1

To improve efficiency, you can remove the unnecessary "return true" statement as it will automatically return true if they match. Instead, retrieve and return the movie information linked to the idUrl.

if (idUrl === idMovie) {
   return idUrl;
 }

Subsequently, utilize this information to access details about the movie.

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

Google Chrome is unable to process Jquery JSON .each() function

My website has a simple chat application that is functioning well. It uses ajax to request data in this manner: $.ajax({ url: "fetch/"+CHAT_SESSION_ID+"/"+LAST_MESSAGE_ID, dataType: "json", cache: false, success: function(data) { if (data.session_ac ...

Customizing the appearance of a JavaScript countdown timer's output on an individual basis

I am currently working on customizing the appearance of a JavaScript date counter. My goal is to style the days, hours, minutes, and seconds individually. Ideally, I want each unit to be right-aligned within its own div, with specific left and top absolute ...

Tips on refining my code and effectively presenting data retrieved from an API in a React application

I have a functional code in place, but I am facing difficulties in enhancing it further. Within my React project, I am utilizing an API call to communicate with an endpoint. The API response varies based on whether it encounters an error or performs succe ...

Obtain the complete path in Vue router by utilizing nested routes

After creating nested routes for Vue Router, I encountered a problem while using the routes to generate a navigation menu. Currently, I am using route.path in 'router-link :to=' which only gives me a part of the path. I want to include the absolu ...

unable to retrieve the response from a POST request sent to a node server

Currently in the process of learning how to utilize node and express, I have embarked on creating a sample website that incorporates both along with the Google translate API. The goal is to explore the numerous features offered by these technologies. Unfor ...

Can the sliding transition effect used in createMaterialTopTabNavigator be implemented in createMaterialBottomTabNavigator as well?

When using createMaterialTopTabNavigator, the transition from screen to screen involves a sliding effect as you press the tabs. On the other hand, createMaterialBottomTabNavigator uses a fading-in animation for its transitions. Is there a way to combine o ...

JQueryMobile 1.3.2 encounters issues with popups following the installation of Chrome version 43.0.2357.65 m update

Is anyone else experiencing issues with the latest version of Chrome "43.0.2357.65 m" causing problems with JQueryMobile 1.3.2? When I try to click on a popup, it suddenly jumps to the top of the page and the scroll bar disappears. This was not an issue in ...

barchart rendered in SVG without the use of any external libraries

Creating a stacked barchart with SVG and HTML without relying on any third-party library has been quite a challenge. Despite searching extensively online, I have not come across a single resource that demonstrates how to build a stacked bar chart using pla ...

Display a dropdown menu when hovering over with a delay

I recently created a basic navigation menu with dropdown functionality using CSS3 initially, but I decided to enhance it by incorporating jQuery to display the dropdown after a set timeframe. However, I am facing an issue where all dropdowns appear when ho ...

Error detected (unresolved promise): Invalid JSON format is causing an Unexpected token < at the beginning of data position (Application built with React/Redux)

I'm in the process of setting up user authentication using JWT. I've successfully created an API for this purpose. Now, my goal is to integrate this authentication API with my React/Redux App. When a user signs up, I trigger an action from my S ...

What is the best way to implement a scrollbar in a specific div rather than the entire window?

Hey everyone! So, I have this window in electronJS with a div where I'm dynamically adding elements using Javascript and the function --> document.createElement('section'). Here's the loop in Javascript to add these elements: for ( ...

Strange issue with Firefox when utilizing disabled attribute as "disabled"

I found a curious bug in Firefox: Check out (unfortunately not reproducible in jsfiddle) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> < ...

When using Vue.js, you may encounter an error message stating that the `document.title` type of 'undefined' cannot be assigned to type 'string' in Typescript

I'm trying to use beforeEnter with Vue Router, but I encountered an error: TS2322: Type 'string | symbol | null | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string&apo ...

Can the environment variables from the .env package be utilized in npm scripts?

I've integrated dotenv into my cypress project and defined variables in a .env file, as shown here: USER=Admin How can I utilize the env variable USER within my npm scripts? "scripts": { "cypress:open": "npx cypress ope ...

The download button consistently targets and downloads the initial SVG in my collection. How can I configure it to target each individual SVG for download?

I'm currently working on a QR code app using React. The goal is for users to submit a form with a value, which will then generate an SVG QR code that can be downloaded. However, I'm running into an issue where the same SVG file is being downloade ...

What could be the reason for the failure of my class isInstance() check

Do you see any issues with the object being an instance of ChatRoom? Let me know your thoughts. Class: export class ChatRoom { public id?: number; public name_of_chat_room: string; public chat_creator_user_id: number; public chat_room_is_active: 0 ...

Numerous Controllers in AngularApp failing to function properly

One issue I'm encountering is that only one out of the two controllers within my app seems to be registered: var app = angular.module('MyModule', []); app.controller('FruitCtrl', function($scope) { $scope.fruits = ['appl ...

Guide on implementing hover, click, and other functions with Lottie in NuxtJS

I am currently working with the 'vue-lottie' plugin, but I'm having trouble finding detailed instructions on how to use it. After obtaining JSON animations from Lordicons, I managed to display them correctly. However, I am facing difficulty ...

Transforming an array of JSON objects into a Knockout observable array containing observable properties

My application utilizes an ajax call to fetch a JSON array. [ {"ID":2,"Name":"Name 1","CreatedOn":"/Date(1432892160000)/"}, {"ID":7,"Name":"Name 2","CreatedOn":"/Date(1432892160000)/"}, {"ID":8,"Name":"Name 3","CreatedOn":"/Date(1432892160000)/"}, {"ID":9 ...

Unable to identify collision in JavaScript's 3D environment

I'm currently using three.js to develop a simulation of Brownian Motion, but I've hit a roadblock when it comes to getting the tiny molecules to collide with each other. This is the snippet I have at the moment: function intersects(sphere, other ...