Why is the Axios instance/function not being passed the argument?

I am currently working on refactoring a next.js App that has a functioning axios call. However, I have encountered an issue with my new function not receiving arguments.

The problem consists of two main components: my next.js page and an external custom module where I write functions to use axios for making calls to the YouTube API to retrieve information.

Here is a snippet of my next.js getStaticProps call:

export async function getStaticProps(context: any) {
  const { slug = "" } = context.params;
  const film = await client.fetch(query, { slug });

  const video = await youtube.grabVideoInfo(film.VideoID);

  return {
    props: {
      film,
      video,
    },
    revalidate: 10,
  };
}

Despite trying different approaches to pass in the video ID as an argument, both attempts failed to call from the API due to an invalid video ID error, indicating that it wasn't being passed in correctly.

First attempt:

const grabVideoInfo = async (videoId) => {
  const videoGrab = axios.create({
    baseURL: "https://www.googleapis.com/youtube/v3/videos?",
    params: {
      headers: { "Access-Control-Allow-Origin": "*" },
      part: "snippet",
      id: videoId,
      key: KEY,
    },
  });

  const query = await videoGrab.get().then(
    (response) => {
      return response.data.items[0];
    },
    (error) => {
      return error.toJSON();
    }
  );
  return query;
};

Second attempt:

const grabVideoInfo = async (videoId) => {
  const videoGrab = axios.create({
    baseURL: "https://www.googleapis.com/youtube/v3/videos?",
    params: {
      headers: { "Access-Control-Allow-Origin": "*" },
      part: "snippet",
      key: KEY,
    },
  });

  const query = await videoGrab.get({ params: { id: videoId } }).then(
    (response) => {
      return response.data.items[0];
    },
    (error) => {
      return error.toJSON();
    }
  );
  return query;
};

Lastly, here is the fully functioning version that I am attempting to refactor:

export async function getStaticProps(context: any) {
  const { slug = "" } = context.params;
  const film = await client.fetch(query, { slug });

  const KEY = process.env.YOUTUBE_API_KEY;
  const conn = axios.create({
    baseURL: "https://www.googleapis.com/youtube/v3/",
    params: {
      headers: { "Access-Control-Allow-Origin": "*" },
      part: "snippet",
      id: film.videoID,
      key: KEY,
    },
  });

  const video = await (await conn.get("videos?")).data.items[0];

  return {
    props: {
      film,
      video,
    },
    revalidate: 10,
  };
}

Any assistance would be greatly appreciated as I try to solve this issue.

Answer №1

Upon examining your refactor, I noticed that it accesses film.VideoId instead of the original implementation which utilizes film.videoId.

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

Replacing text using regex results in whitespace

How can I eliminate text and spaces? For instance: http://www.exampleweb.com/myprogram.rar http://www.examplebackup.com/mybackups.rar http://www.exampleweb.com/myprogram1.rar I have used something similar to remove the second line: http://www.exampleba ...

How can we deliver pure JS, HTML, and CSS content without relying on static HTML pages?

Looking to create a fast app prototype without using React or Vue? I'd like to avoid simply making an html and js file imported within it. Can npm packages, SCSS be used while programming vanilla Javascript minus a framework? ...

what is the process for creating a dynamic display slide in bxslider?

I am trying to create a flexible length display using bxSlider. Here is the code I have so far. JS var duration = $('ul > li > img').data("bekleme"); $(document).ready(function () { $('.bxslider').bxSlider({ ...

Having trouble configuring webpack for CSS Modules? You may be dealing with an invalid configuration object

Encountering an issue while setting up webpack for CSS Modules... An error message is appearing stating that the configuration object is invalid. It seems that the output path specified as "build" is not an absolute path, which is required. Below are ext ...

Establishing the folder organization for Express Handlebars

I work with NodeJs, Express, and Handlebars. The main file for my server is named app.js const express = require('express'); const exphbs = require('express-handlebars'); const app = express(); app.engine('handlebars', ex ...

Node.js error: HTML audio file returns 404 on server but plays fine when accessed directly as an HTML file

I'm facing an issue with an HTML page that contains an autoplay tag for a song (the file is in mp3 format). When I open the page as a local file on my Mac, the song plays fine. However, when I try to run it using a node.js file with socket.io, the son ...

Node.js tutorial: Building routes with regex and implementing redirects

Can anyone provide guidance on how to utilize route and redirect URLs with parameters in Node.js using Express? If the URL is xyz/?s=test, it should redirect to indexRouter. If the URL is xyz/, it should also redirect to indexRouter. I'm facing issu ...

A comprehensive guide on iterating through an array in JavaScript

Currently, I am facing an issue while trying to iterate over an array of Objects in React that have been fetched from Django. The object is stored as an array, but when attempting to map it, I encounter a typeerror stating "cannot read property map of unde ...

What are the best methods for retrieving data from a subcollection in Firebase/Firestore with maximum efficiency

Utilizing Firestore to store posts with simple properties like {title: 'hi', comment: true} has been a seamless process for fetching user-specific data, given the structure of my collection: posts/user.id/post/post.name. An example would be posts ...

Tips for organizing Protractor promises

I am currently experimenting with determining if an element is positioned at the bottom of a page in Protractor/Webdriver using promises. However, I feel like my current approach is quite messy and there must be a cleaner way to achieve this. describe(&ap ...

Error message: "Ajax script is failing to run"

Currently I am delving into the world of Ajax and have encountered a minor issue. I've been attempting to make a POST request to a Django backend using ajax, but strangely enough, the alert isn't showing up on screen. Furthermore, upon inspectio ...

Encountering Uncaught Syntax Error when attempting a request with JSON parameters

Currently, I am using Fetch to send a post request to my server while including some additional information. Here's the code snippet: var rating = document.getElementById("rating"); var ratingValue = rating.innerHTML; fetch("/films",{ method: "po ...

Incorporating the JqueryUI menu into an AngularJS project

We are facing an issue with integrating JqueryUI menus into our AngularJS application. In our Layout.js file, we are attempting the following: OURApp.controller('leftBarCtrl', ['$scope', function ($scope) { $scope.init = function ( ...

Mocking a Promise-returning dependency for a React Component in Jest

There's a React component I'm working on that has a dependency like so: import { fetchUsers } from '../../api/'; This function is a utility that returns a Promise. My challenge lies in trying to mock this dependency using Jest. I&apo ...

Converting a floating point number to a 4-byte hex string in JavaScript and reversing the process

I have received a hexadecimal data from the server side that is supposed to be in float format. I am trying to convert these hexadecimals into floats using JavaScript, but so far I have been unable to find a suitable method. Can anyone provide assistance ...

I am looking to retrieve information from mongodb and then transform it into a JSON object using node.js. Can you guide

I am on a mission to retrieve data from a MongoDB database and transform it into a JSON object in Node.js. The goal is to be able to easily manipulate this data as if it were a simple JSON object. Here's the current code snippet I'm working with: ...

What is the best way to display or hide specific tables depending on the button chosen?

Being fairly new to JavaScript, I find myself unsure of my actions in this realm. I've successfully implemented functionality for three links that toggle visibility between different tables. However, my ideal scenario would involve clicking one link t ...

Refresh the page when the parameter in the URL changes

I'm encountering a small issue that I can't seem to resolve. Currently, I am on the following page: http://example.com:8080/#/user/5ad142e8063ebb0537c5343e There is a link on this page that points to the URL below: http://example.com:8080/#/u ...

What is the correct way to set up Cypress intercepts within a beforeEach function?

As a newcomer to Cypress, I find some aspects of it challenging despite its apparent simplicity. I am facing a specific issue: const componentsRouteMatcher = { pathname: '/component-management/api/components', query: { size: &apos ...

Error 404: This page seems to have gone missing. Django and react-router

Struggling to integrate reactjs and react-router (1.x) with my Django project. I'm finding it challenging to make everything work together seamlessly. For more details, you can check out the project on GitHub: https://github.com/liondancer/django-che ...