TransWikia.com

TypeError: Cannot read property 'split' of undefined in React js

Stack Overflow Asked by user13427179 on January 22, 2021

Hii guys i am trying to building a music player application then i am getting an error like Cannot read properly split undefined don’t know what’s going on wrong please try to fix my error

enter image description here

   import axios from "axios";
  
    export const selectRandomKey = () => {
      const keys = process.env.REACT_APP_YouTube_Keys.split(""); //we are splitting the api keys to make an array
      const random = Math.floor(Math.random() * Math.floor(keys.length)); //this will get a random number
      return keys[random];
    }
    
    export default axios.create({
      baseURL: "https://www.googleapis.com/youtube/v3",
      params: {
        part: "snippet",
        videoCategoryId: "10",
        type: "video",
        key: selectRandomKey()
      }
    });

One Answer

You are getting this error because the process.env.REACT_APP_YouTube_Keys may be undefined. Instead of directly using split, you can do like const YOUTUBE_KEYS = process.env.REACT_APP_YouTube_Keys || ''; and const keys = YOUTUBE_KEYS.split("");

Correct answer by Sarang PM on January 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP