Using Vue.js, you can set a method to return data directly to

Having just begun my journey with Vue, I find myself in a bit of a predicament. As part of my learning process, I am developing an app for tracking episodes in TV series. The initial step involves searching for series and adding them to a database. When conducting the search, the results appear like this: https://i.stack.imgur.com/QuOfc.png

Below is the code snippet containing the template and script:

<template>
  <div class="series">
    <ul>
      <li v-for="item in series" :key="item.id">
        <img :src="image_url+item.poster_path"/>
        <div class="info">
          {{item.name}}
          <br/>
          <h5>{{item.id}}</h5>
          Start Date: {{item.first_air_date}}
          <br/>
          {{getEpisodeNumber(item.id)}}
          <br/>
          {{getSeasonNumber(item.id)}}
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "series",
  props: ["series"],
  data() {
    return {
      image_url: "https://image.tmdb.org/t/p/w500",
      api_key: {-api key-},
      episode_url: "https://api.themoviedb.org/3/tv/",
    }
  },
  methods: {
    async getEpisodeNumber(showID) {
      const json = await fetch(this.episode_url + showID + this.api_key)
        .then((res) => { return res.json() })
        .then((res) => { return res.number_of_episodes })
      return await json
    },
    async getSeasonNumber(showID) {

      const json = await fetch(this.episode_url + showID + this.api_key)
        .then((res) => { return res.json() })
        .then((res) => { return res.number_of_seasons })
      return await json;
    }
  },
}
</script>

The issue lies in the fact that the methods are supposed to provide a number as output, but instead return an object, likely a promise object. Despite attempting various strategies, such as using console.log within the methods, I have been unable to access the desired value. Each attempt has ended in failure, leaving me stuck on how to proceed.

Answer №1

After creating a custom component named display, I passed item.id as a parameter to this new component. Within the display component, I utilized another fetch() method to retrieve show data once more, resulting in the desired functionality.

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

Adjusting the focus of an element with jQuery based on coordinates and offset values

jQuery.fn.getCoord = function(){ var elem = $(this); var x = elem.offset().left; var y = elem.offset().top; console.log('x: ' + x + ' y: ' + y); ); return { x, y }; }; This custom jQuery funct ...

What is the way to execute a function *once* all my ajax call functions have finished?

I am utilizing jQuery to execute some ajax requests: var information = {}; function process_information(item){ information[item.id] = item; } function perform(){ var calls = []; for(var i = 0; i < 10; i++){ var call = $.get(URL, ...

The collaboration of Node.js and React.js on a single server

Separate ports are used for Node and React, but API requests from the React app can be proxied to the Node URL. I have opted not to implement server-side rendering for React in order to serve the React app. Instead, I build the React app each time there i ...

Iterate through and conduct conditional verification

In my project using AngularJS and HTML, I have created a table to display records. I am looking for a way to iterate through the column values and strike through any value in the column that meets a certain condition. For example, in the demo provided her ...

Retrieve the value of the element and combine them together (The parseFloat function may not work as expected

The following code is what I am currently working with: var num1=$("#num1").val(); //the num1 value is 12.60 //html code for this number <input id="num1" value="12.60" /> num1=parseFloat(num1).toFixed(2); var num2=$("#num2").text(); //the num2 valu ...

Issue with margins of sections when attempting to activate menu class while scrolling

My Objective: I want to create a website where the menu highlights the section that is currently being viewed as the user scrolls up and down. Progress So Far: I have successfully implemented a functioning menu that changes to "active" when the correspo ...

The XML information vanished during the transformation into JSON format

After converting XML to JSON using multiple conversion libraries, I noticed that the property name attributes and Item name attributes were lost. Why is this happening? Does anyone have suggestions on how I can modify my XML to make it more compatible for ...

There seems to be a complete absence of rendering in this particular vue

Upon initializing a fresh Vue project using Vue CLI 3 and removing all default views and components, I proceeded to add two new views. To my surprise, these views did not render when changing the URL; instead, I was met with a blank page and no error messa ...

Parsing PHP array using JavaScript

$namesSelect = "SELECT username FROM users"; $names = mysql_query($namesSelect); $nameCheck = mysql_fetch_array($names) This code snippet retrieves all usernames from the users table and stores them in the $names array. I am now faced with the task of ...

Transforming two child arrays within an object into a single array using Ramda

I am looking to transform an object into an array. The object I have is structured like this: const data = { t1: [ {"a": 1, "a1": 2}, {"b": 3, "b1": 4}, {"c": 5, "c1": 6} ], t2: [ {" ...

Ways to leverage the power of Reactivity APIs in Vue.js

In my Vue application, I am trying to create a Drop Down Menu by using a variable called "showDropDown". The idea is to use a v-if directive to check if the value of showDropDown is true, and then display the menu accordingly. Additionally, I want to imp ...

Create unique error messages using Vue validators

Two fields named text and email are included in my template. Below is the structure of the template: HTML <!DOCTYPE html> <html> <head> <title>Vue - Validations</title> <script src="Libraries/vue/vue.min.js&qu ...

Using $emit in VueJS to set exclusive active components

I'm facing a challenge with setting unique active Components using $emit in VueJS. My goal is to have Tab A become active when clicked on in the tabbar component, without affecting the activation status of Tab B, and vice versa for Tab B. I apprecia ...

The persistent problem with constantly polling the $.ajax request

One issue I'm facing involves a continuous polling $.ajax request. The challenge lies in initiating it immediately first, and then running it at intervals set in the setTimeout call. Take a look at the example code here. myObj = {}; var output = ...

Create a new template following rendering in VueJS

I am facing an issue while using the datatable jQuery plugin with vueJs. Whenever I try to use the render function of the datatable and include vueJs code like @click, it doesn't get interpreted. Instead, in the source code, @click is visible but noth ...

What is the reason why Prettier does not automatically format code in Visual Studio Code?

After installing and enabling ESLint and Prettier in my Nuxt application, I made the switch to Visual Studio Code. However, when I open a .vue file and use CMD+ Shift + P to select Format Document, my file remains unformatted. I even have the Prettier ex ...

Error 404: Page not found. Sorry, we couldn't locate the Node JS and Express resource

I have been trying to access the routes /api and /api/superheroes, but I keep encountering an error message whenever I try. Not Found 404 NotFoundError: Not Found at C:\Users\mikae\Desktop\Project\node-express-swig-mongo\a ...

Checking if a phone number begins with a zero using a regular expression

Is there a way to ensure that numbers entered into a field always start with a 0? Initially, I thought the company wanted to mandate entering 0 first, but I believe there might be a more elegant solution. function validateNumber(dataValues, setErrors) ...

What is the way to display the final list item when clicking in jQuery?

I am attempting to achieve a specific behavior where clicking on a button will trigger the content below to scroll in such a way that only the last item in the list is visible. I have been using jQuery for this functionality, but unfortunately, it is not ...

Using ChartsJs to visualize input data formatted in the German data format

I am relatively new to working with Charts.js, but I will need it to generate some visually appealing graphs for my website. In the background, I have a Django project running that calculates a specific set of numbers for me. Due to the language setting in ...