【问题标题】:TypeError: Cannot read property 'split' of undefined in React jsTypeError:无法读取 React js 中未定义的属性“拆分”
【发布时间】:2023-03-24 15:30:01
【问题描述】:

嗨,伙计们,我正在尝试构建一个音乐播放器应用程序,然后我收到一个错误,例如无法正确读取拆分未定义不知道发生了什么问题,请尝试修复我的错误

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

【问题讨论】:

  • process.env.REACT_APP_YouTube_Keys 控制台并检查是否正在填充值
  • 不建议在前端使用密钥。

标签: javascript reactjs api react-hooks


【解决方案1】:

您收到此错误是因为 process.env.REACT_APP_YouTube_Keys 可能未定义。而不是直接使用拆分,你可以这样做 const YOUTUBE_KEYS = process.env.REACT_APP_YouTube_Keys || '';const keys = YOUTUBE_KEYS.split("");

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 2022-11-17
    • 2020-12-24
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多