TypeError:无法读取React js中未定义的属性'split'

时间:2020-11-07 05:13:44

标签: javascript reactjs api react-hooks

Hii,我正在尝试构建音乐播放器应用程序,然后出现错误,例如无法正确读取未定义的拆分不知道是怎么回事,请尝试解决我的错误

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()
      }
    });

1 个答案:

答案 0 :(得分:0)

您收到此错误,因为process.env.REACT_APP_YouTube_Keys可能未定义。您可以像 const YOUTUBE_KEYS = process.env.REACT_APP_YouTube_Keys || '';const keys = YOUTUBE_KEYS.split("");