我在使用 Spotify Web 应用程序时遇到了奇怪的错误。我尝试将播放列表保存到我的帐户,该帐户必须id从https://api.spotify.com/v1/me端点获取元素,然后将播放列表应用到您的帐户。除此之外,一切看起来都很好,除了提取到该端点时会抛出错误:
Spotify.js:81 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': The provided value is not of type '(sequence<sequence<ByteString>> or record<ByteString, ByteString>)'
我以前从未见过此错误,并且不确定为什么会发生。方法findUserId是:
findUserId() {
if(accessToken === undefined) {
this.getAccessToken();
}
console.log(accessToken);
let userId;
fetch(`https://api.spotify.com/v1/me`, {headers: `Bearer ${accessToken}`}
).then(response => {return response.json()}
).then(jsonResponse => {
userId = jsonResponse.id;
});
console.log(userId);
return userId;
}
Run Code Online (Sandbox Code Playgroud)
首先,您必须Authentication在 内设置标头headers。另外,fetch它是异步的,这意味着您尝试userId在网络请求完成之前进行日志记录。要解决此问题,请将日志放入第二个then回调中并返回fetch:
findUserId() {
if (accessToken === undefined) {
this.getAccessToken();
}
console.log(accessToken);
return fetch(`https://api.spotify.com/v1/me`, {
headers: { Authentication: `Bearer ${accessToken}` }
})
.then(response => response.json())
.then(jsonResponse => {
userId = jsonResponse.id;
console.log(userId);
return userId;
});
}
Run Code Online (Sandbox Code Playgroud)
findUserId然后你可以像这样使用:
async otherFunction() {
const userId = await this.findUserId();
console.log(userId);
}
Run Code Online (Sandbox Code Playgroud)
或者像这样:
otherFunction() {
this.findUserId().then(userId => console.log(userId));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13446 次 |
| 最近记录: |