简体   繁体   English

类型错误:在使用 spotify 的 api 时无法读取未定义的属性

[英]TypeError: Cannot read property of undefined while using spotify's api

I'm trying to link the spotify's API to my app, but when i try to run the code, the following error appears :我正在尝试将 spotify 的 API 链接到我的应用程序,但是当我尝试运行代码时,出现以下错误:

在此处输入图片说明

here's the code: First, i use the useEffect to put the script tag and link the API, then, i declare the var Spotify , but when the page loads it didn't find the property Player in Spotify这是代码:首先,我使用useEffect来放置脚本标签并链接 API,然后,我声明了 var Spotify ,但是当页面加载时,它没有在Spotify找到属性Player

 import React, { useEffect } from 'react' function SpotifyPlayer() { var Spotify:any useEffect(() => { const script = document.createElement('script'); script.src = "https://sdk.scdn.co/spotify-player.js"; script.async = true; document.body.appendChild(script); return () => { document.body.removeChild(script); } }, []); (window as any).onSpotifyWebPlaybackSDKReady = () => { const token = '[token]'; const player = new Spotify.Player({ name: 'Web Playback SDK Quick Start Player', getOAuthToken: (cb:any) => { cb(token); } }); // Error handling player.addListener('initialization_error', ({message}:any ) => { console.error(message); }); player.addListener('authentication_error', ({ message }:any) => { console.error(message); }); player.addListener('account_error', ({ message }:any) => { console.error(message); }); player.addListener('playback_error', ({ message }:any) => { console.error(message); }); // Playback status updates player.addListener('player_state_changed', (state:any) => { console.log(state); }); // Ready player.addListener('ready', ({ device_id }:any) => { console.log('Ready with Device ID', device_id); }); // Not Ready player.addListener('not_ready', ({ device_id }:any) => { console.log('Device ID has gone offline', device_id); }); // Connect to the player! player.connect(); }; return <h1>Spotify Player</h1> } export default SpotifyPlayer

-> I'm using React too. -> 我也在使用 React。

Your typing for Spotify any doesn't have any properties.您为 Spotify any打字没有任何属性。 ;) Best bet would check if Spotify has a type library and use it here. ;) 最好的办法是检查 Spotify 是否有类型库并在此处使用它。 Otherwise you can use a type with the properties you pointing to.否则,您可以使用具有您指向的属性的类型。

type SpotifyType {
  properties: any; // better to look for the typing config
}

var Spotify:SpotifyType

Another option is to try !另一种选择是尝试! Nonnull assertion非空断言

const player = new Spotify!.Player({
      name: 'Web Playback SDK Quick Start Player',
      getOAuthToken: (cb:any) => { cb(token); }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spotify Web API:未捕获(承诺)TypeError:无法读取未定义的属性“setState” - Spotify Web API: Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined TypeError:从 API 获取时无法读取未定义的属性“映射” - TypeError: Cannot read property 'map' of undefined while fetching from API 未捕获的类型错误:使用 Rcipe API 时无法读取未定义的属性“映射” - Uncaught TypeError: Cannot read property 'map' of undefined while using Rcipe API Uncaught TypeError:无法读取未定义的属性“ chooseEntry”(在使用fileSystem API开发chrome扩展时) - Uncaught TypeError: Cannot read property 'chooseEntry' of undefined (while developing a chrome extension using fileSystem API) TypeError:无法读取未定义的属性“长度”-使用安全帽进行部署时 - TypeError: Cannot read property 'length' of undefined - While deploying using hardhat Uncaught TypeError:使用transitionToRoute时,无法读取undefined的属性“ enter” - Uncaught TypeError: Cannot read property 'enter' of undefined , while using transitionToRoute TypeError: Cannot read property 'then' of undefined -- 在使用 then 时出现此错误 - TypeError: Cannot read property 'then' of undefined --got this error while using then 未捕获的类型错误:无法使用 Google 图书 API 读取未定义的属性“1” - Uncaught TypeError: Cannot read property '1' of undefined using Google Books API 类型错误:无法读取未定义 API 的属性“ft” - TypeError: Cannot read property 'ft' of undefined API TypeError:无法使用React读取undefined的属性 - TypeError: Cannot read property of undefined using React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM