Hii,我正在尝试构建音乐播放器应用程序,然后出现错误,例如无法正确读取未定义的拆分不知道是怎么回事,请尝试解决我的错误
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()
}
});
答案 0 :(得分:0)
您收到此错误,因为process.env.REACT_APP_YouTube_Keys
可能未定义。您可以像
const YOUTUBE_KEYS = process.env.REACT_APP_YouTube_Keys || '';
和
const keys = YOUTUBE_KEYS.split("");